Creating Simple oEmbed-Based WordPress Shortcodes

Say you wanted to create a shortcode like this: [youtube id="991WcoEPwb8"]

And instead of manually creating the HTML yourself, you wanted to use YouTube’s oEmbed provider to get the HTML. While that’s easy to do using WordPress’ existing functions (wp_oembed_get() for example), you must implement your own caching of the result as WordPress’ oEmbed class does not include any caching of it’s own.

However, WordPress comes with an [embed] shortcode that’s also secretly used for the shortcode-less embeds. A cool trick is to make that shortcode’s existing code (complete with caching) work for you! This post explains how to do it.

First, the code:

add_shortcode( 'youtube', 'my_youtube_shortcode' );

function my_youtube_shortcode( $atts ) {

	// We need to use the WP_Embed class instance
	global $wp_embed;

	// The "id" parameter is required
	if ( empty($atts['id']) )
		return '';

	// Construct the YouTube URL
	$url = '' . $atts['id'];

	// Run the URL through the  handler.
	// This handler handles calling the oEmbed class
	// and more importantly will also do the caching!
	return $wp_embed->shortcode( $atts, $url );

We start by gaining access to the WP_Embed instance that we’ll be making use of and then making sure we have the required video ID (you can do whatever you want here). We then create the full URL to the video that oEmbed will need. Lastly the real time saver — we pass the attributes (“id” will be ignored) and the constructed URL to the [embed] shortcode handler which will fetch the result and cache it.

Simple, huh?

One thing to note though: if you’re using this along with a non-default oEmbed provider, you’ll also need to whitelist it using wp_oembed_add_provider().

So Apparently WordPress Can Guide Missiles

First off, all credit goes to DD32 for this. He spotted it and I’ve been laughing ever since at how funny this is.

The following screenshots are from episode 5 of the British mini-series Strike Back:

If you don’t recognize the code, it’s from one of WordPress’ Javascript files called post.js.

Mark Jaquith was even kind enough to port their changes back into the WordPress core. Hilarious.

svn blame

The other day I wanted to figure out when a particular line in WordPress was changed. I wanted to know this so I could find the commit message and ticket that went along with it so I could figure out the reasoning behind the change. How do you go about doing that though?

It turns out that svn blame is the command you want. It will output something like this:

$ svn blame
     3      sally This is a README file.
     5      harry You should read this.

So the first line was last modified by sally in revision 3 and the second line was last modified by harry in revision 5. Awesome huh?

Props to @ArtemR, @westi, and @zamoose for informing my noob ass on how to do this!

Twitter Followers Count Snippet for WordPress

UPDATE: This code no longer works as Twitter retired v1.0 of their API. v1.1 of their API requires authentication in order to access it. I have no plans to update this code for their new API. Sorry.

Konstantin Kovshenin posted some code on his blog for how to display how many Twitter followers someone has. While the idea was good, I think he went about the implementation in the non-best method. So, for fun and because I don’t post enough code snippets here on this blog, I thought I’d post how I would display how many followers someone has on Twitter. 🙂

function viper_twitter_followers( $username = 'Viper007Bond' ) {

	// Just to keep the code below cleaner, create the cache key now
	$cache_key = "viper_twitter_followers_{$username}";

	// First we look for a cached result
	if ( false !== $followers = get_transient( $cache_key ) )
		return $followers;

	// Okay, no cache, so let's fetch it
	$result = wp_remote_retrieve_body( wp_remote_get( '' . urlencode($username) ) );

	// Check to make sure we got some data to work with
	if ( empty($result) ) {
		// Cache the failure for 1 min to avoid hammering Twitter
		set_transient( $cache_key, 0, 60 );

	// Parse the data
	$data = json_decode( $result );

	// Make sure we were able to parse it
	// If not, cache the failure (like above)
	if ( !isset( $data->followers_count ) )
		set_transient( $cache_key , 0, 60 );

	// Success! Cache the result for an hour.
	$followers = (int) $data->followers_count;
	set_transient( $cache_key, $followers, 3600 );

	return $followers;

echo 'I have ' . viper_twitter_followers( 'Viper007Bond' ) . ' followers on Twitter!';

Reading Material:

If you have any questions about the above code, let me know and I’ll do my best to answer them. 🙂

Calling All Flowplayer Users

Do you use Flowplayer on your website and have any additional configuration parameters? If so, can you please leave a comment on this post saying in what way you have customized the player (autoplay, etc. etc.).

I’m currently recoding my Video Quicktags plugin to use Flowplayer and I need to know what configuration options I need to allow the user to specify. I’m new to the player, so I don’t want to leave anything out that a lot of users will want to use. (I already have support for the commercial version on my todo list.)


Posionous People

If you’re involved in the WordPress community (or any open-source community for that matter) and have never sat down and watched (or at least listened to) this presentation by two of the founders of the Subversion project, I strongly recommend you do. It is an hour very, very well spent.

Thanks to Ryan McCue for sharing the video with me.