URL: http://www.viper007bond.com/2010/06/23/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 (wpoembedget() 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.
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 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 ); }
You need to login to post a comment.
