Starting in Version 2.9 WordPress added a super cool feature that lets you embed videos, images and other media by simply adding the source url in the post. So for example you could at the url “https://www.youtube.com/watch?v=PaUFSW3bxF8″ and the video would be automatically added to your post. WordPress currently support auto-embeds from these sources:
- YouTube (only public videos and playlists – “unlisted” and “private” videos will not embed)
- Flickr (both videos and images)
- WordPress.tv (only VideoPress-type videos for the time being)
- SmugMug (WordPress 3.0+)
- FunnyOrDie.com (WordPress 3.0+)
- Twitter (WordPress 3.4+)
I wanted to this feature to my Coming Soon plugin but it was not obvious on how to do so. I started hunting down the function that enabled this functionality on the post by searching for all the filters that ran on the template tag “the_content”. I found that on line 1056 in the file /wp-includes/media.php the following code was called:
// Attempts to embed all URLs in a post if ( get_option('embed_autourls') ) add_filter( 'the_content', array(&$this, 'autoembed'), 8 );
The method autoembed is part of a class called WP_Embed which is defined as “API for easily embedding rich media such as videos and images into content.”
So this was the function I need to pass my custom field content into to get the auto embed urls. I discover this was class was in the $GLOBALS array. so now all I had to do was call the method. Here’s the code.
if(isset($GLOBALS['wp_embed'])) $content = $GLOBALS['wp_embed']->autoembed($content);
This would run your content through the oEmbed process of getting the html for the media urls.
If anyone knows of a better way to do this please let me know in the comments.