WordPress: Embed Youtube Feed With a Simple Shortcode
UPDATE: Looking for more functionality and an easier installation method?
Check out my WordPress plugin: Youtube Feeder
It's based on this code and has a LOT more features
WordPress shortcodes are great. They let you perform complex operations or display dynamic content with just a tiny bit of text in any page or post.
I recently needed a way to embed the most recent videos from a Youtube channel (very dynamic information) in a WordPress page, so I decided to write a shortcode for it.
The code is actually pretty simple. Just add the following to your theme's functions.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | function youtube_feed_shortcode($atts) { // Defaults: extract(shortcode_atts(array( 'user' => 'flamadiddle86', // youtube user 'limit' => 5, // maximum number of videos 'height' => 385, // video height 'width' => 480 // video width ), $atts)); $data = @json_decode(file_get_contents('http://gdata.youtube.com/feeds/api/users/'.$user.'/uploads?alt=json'), TRUE); $counter = 0; $content = '<div class="youtubefeed">'; foreach($data['feed']['entry'] as $vid) { $url = $vid['media$group']['media$content'][0]['url']; $title = $vid['title']['$t']; $ycontent = $vid['content']['$t']; $content.= '<object width="'.$width.'" height="'.$height.'">'. '<param name="movie" value="'.$url.'"></param>'. '<param name="allowFullScreen" value="true"></param>'. '<param name="allowscriptaccess" value="always"></param>'. '<embed src="'.$url.'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'.$width.'" height="'.$height.'"></embed></object>'. '<div class="youtubetitle">'.$title.'</div>'. '<div class="youtubecontent">'.$ycontent.'</div>'."\n"; $counter++; if($counter == $limit) { break; } } $content .= '</div>'; return $content; } add_shortcode('youtubefeed', 'youtube_feed_shortcode'); |
And it couldn't be easier to use. The videos you see below were embedded simply by putting this code in the post:
1 | [youtubefeed] |
The shortcode is completely configurable. The following code would have resulted in the exact same output:
1 | [youtubefeed user="flamadiddle86" limit="5" height="385" width="480"] |
You can change any option as needed.
You might have noticed the code gives each element a particular class to make it CSS friendly. All you have to do is add CSS rules for three classes: .youtubefeed, .youtubetitle, and .youtubecontent.
My stylesheet looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | .youtubefeed { margin: 10px; text-align: center; } .youtubetitle { font-size: 1.6em; font-weight: bold; margin: 3px; } .youtubecontent { margin-bottom: 20px; } |
A quick note about requirements:
- Your server's PHP configuration needs to have allow_url_fopen set to On.
- You'll also need the PHP JSON extension
Most hosts meet those requirements.
So there you have it. It's a quick and dirty way to get functional, always up-to-date Youtube streams in your WordPress posts and pages.
Possibly Related posts:


Pingback: Link: Embed YouTube Channel’s Feed in WordPress (Shortcode) - Professional Web Developer, Designer - Wordpress Theme Design - Mobile Website Design - Ottawa, Canada - Wilder Tweedale - Web Designer - Blog
Pingback: embedding you tube videos wordpress - DesignersTalk