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 = 'http://www.youtube.com/watch?v=' . $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().

8 thoughts on “Creating Simple oEmbed-Based WordPress Shortcodes

  1. i was looking for solutions to alter the youtube oembed code to the new inline frame version. still looking for documentation on that.

    wordpress support forum post: http://goo.gl/9HR4 in case anyone is interested in answering.

  2. Thank you so much for sharing. This lightweight approach means I can remove the super fancy, and bloated youtube plugin that I’m currently using.
    Have a great day!

    PS. Grab a Terminator Stout for me please? I no longer live in the area, and driving 3,000 miles to get one might be a bit much 🙂

  3. Thanks for this snippet. I’ve come across an odd issue with it. I’m using very similar code to the above, but it seems to show a Vimeo video when there’s no “www” in the URL, but just returns the URL when there is “www” in it.

    echo $wp_embed->shortcode( array( "width" => 450, "height" => 253 ), $url )

    Have you seen anything like this? Any ideas?

  4. Sorry if this is a dumb question…but my brain is kind of small today. If I just throw my url between [embed] tags, will wordpress cache it? Also I was just wondering…is there any way to get more video data than just the HTML from wordpress’s built-in oembed functionality? For example, could I pull a video’s title, description, etc. using something built-in in wordpress? Thanks for sharing…you saved me from having PEAR and Services_oEmbed explode my tiny brain.

    • I somehow overlooked this comment a full year ago but I’m still going to reply. Better late than never? 🙂

      If you throw any supported URL between the [embed] tags, then it will work and be cached (to post meta). Unsupported URLs will be hotlinked.

      WordPress is only interested in the returned HTML but you can read whatever data you want from a provider’s oEmbed endpoint using the oEmbed class. You’ll need to cache that on your own though, etc.

Comments are closed.