Want to generate a massive…

Want to generate a massive amount of output from the wp shell? Save it to a text file using tee.

wp shell --url=preprod.site.ca | tee output.md
Advertisements

For counting hours in Excel

[h]:mm – this allows you to count hours over 24 hours.

How to view a Facebook Instant Article

https://a8c.slack.com/archives/C029H6R8C/p1502803073000355

Debugging filters

This is something you can do when you suspect a filter is causing a problem, but you don’t know which one.

Plugins in mu-plugins are activated automatically. Adding an action on “all” will make your function run on every hook. Beware, you’ll get a lot of output. It goes to /tmp/php-errors.

<?php ///wp-content/mu-plugins
define( 'WP_DEBUG_LOG', true );

function shannon() {
        error_log( "Post variables or whatever else can go here" );
        error_log( current_filter() );
}

add_action('all', 'shannon');

Statics and globals

WordPress has a known global variables problem. We use them way too much. People try to mitigate this bad practice by using static variables instead of globals, since there are plenty of situations where a static will work just as well.

It can be tricky to grasp the distinction between static and global variables, but this is the best explanation I’ve found so far.

The lexical scope of a static static variable is restricted to the function body – you cannot access the variable outside the function. However, its value will be remembered across multiple calls of the same function.

Global variables exist in global scope and can be accessed from anywhere in your code (you have to use the global keyword or $GLOBALS array inside functions though)

knittl on StackOverflow

This is a great example of where you might use a static. fm_calculate_context(); is performance-intensive, but the result, $calculated_context, is used often.

Instead of polluting the $GLOBALS array, the static keeps the calculated value safe and available in fm_get_context(), and it doesn’t have to be recalculated each time.

function fm_get_context( $recalculate = false ) {
	static $calculated_context;
	if ( ! $recalculate && $calculated_context ) {
		return $calculated_context;
	} else {
		$calculated_context = fm_calculate_context();
		return $calculated_context;
	}
}

Context

Instagram Importer for WordPress

I’m in charge of creating and maintaining the website for a new family business. It’s a vintage furniture and bookshop in Als, Denmark, called Als Genbrug & Antikvariat. I’m not the best at either web design or social media, but one thing I know for sure is that the more interesting content you can come up with, the more it drives traffic. Once you have traffic, you can drive it straight to your shop.

To that end, I wanted to use Instagram to show people the interesting stuff we have in the shop, not to mention the harrowing story of how we built the place from the ground up in an old warehouse in about a month.

I love Instagram for the ability to throw up good content quickly and easily, connect with people who care about what you care about, and the focus on pictures over text. However, I don’t love it for the walled-garden aesthetic and the Facebook overlords.

A good compromise, I thought, would be to display Insta posts on a WordPress site. I tried a couple of plugins that I found by searching for “Instagram Feed” in the plugins menu – Instagram Feed by Smash Balloon, and AccessPress Instagram Feed by Access Press Themes.

Both were very easy to set up and get a feed into a post or page using shortcodes, but neither did exactly what I wanted.

Searching for “Instagram Importer” gave much more promising results. This plugin by Automattic’s own Beau Lebens does the trick perfectly. Keyring Social Importers can connect to several web services and use their public APIs to download the content, save it to your database, and display each item as a post. You also need the Keyring app by the same author.

This does exactly what I want, and now I’m able to control all my content by hosting it on my own server, with identical content to what I posted on Instagram without having to make the same post twice.

The site isn’t done yet (9 July 2017), but check out our Insta feed in the meantime, or go try out Beau’s plugin yourself.

Keyring Social Importers
Keyring

PHP Trivia

Here are some PHP functions that I had to look up last week.

tempnam() – creates a new file unique name plus a given prefix in the current directory. You have to delete it yourself, but the return value is the full path to the temp file so it’s easy.

ob_start() – starts output buffering, which you might use if you want to do something with command line output generated by a function like exec() or passthru, speaking of:

passthru() – is like exec except instead of storing the output to a variable, it is outputted directly to the screen or browser.

xdiff_string_diff() – diff’s two strings and returns unified output similar to the CLI diff command.