How to display a Youtube Video Thumbnail

Share & Comment

Youtube Wordpress Shortcode

Youtube WordPress Shortcode

If you often use Youtube videos in your blog posts, you would have always wanted to display a thumbnail from the video on the archive or homepage for your readers. Today I will tell you how to do that using a WordPress shortcode.

You will need to copy the following code in your theme’s “functions.php”. Thesis Theme users should copy this code to “custom_functions.php”.

[php]
/*
Shortcode to display youtube thumbnail on your wordpress blog.
Usage:
[youtube_thumb id="VIDEO_ID" img="0" align="left"]
VIDEO_ID= Youtube video id
img=0,1,2 or 3
align= left,right,center
*/
function wp_youtube_video_thumbnail($atts) {
extract(shortcode_atts(array(
‘id’ => ”,
‘img’ => ’0′,
‘align’=>’left’
), $atts));
$align_class=’align’.$align;
return ‘</pre>
<img id="" src="<a href=" alt="" />
<pre>http://img.youtube.com/vi/’.$id.’/’.$img.’.jpg&quot; alt="" class="’.$align_class.’" />’;
}
add_shortcode(‘youtube_thumb’, ‘wp_youtube_video_thumbnail’);
[/php]

Once done, you can use the shortcode. It accept 3 parameters: video ID, image size, and alignment. This shortcode uses Youtube API to create a thumbnail from the video.

[php][youtube_thumb id="rNWeBVBqo2c" img="0" align="center"][/php]

Thanks to Gunay for the great code!

Vijay Sharma

Vijay Sharma is an aspiring tech blogger and internet consultant focused on helping the community with the tips and tricks that can help you build better web business.