FAQ

Frequently Asked Questions

How to display a thumbnail from a Youtube video using a shortcode – CodeRevolution

Are you often displaying Youtube videos on your blog? If yes, what about displaying a preview thumbnail to your readers? Here is a great shortcode that will display a thumbnail from a Youtube video, using Youtube api.

First, you need to create the shortcode. To do so, copy the code below and paste it into your functions.php file.

 /*     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 '<img src="https://web.archive.org/web/20180630/CodeRevolution.com/<a%20href="http://img.youtube.com/vi/'.$id.'/'.$img.'.jpg&quot" rel="nofollow">http://img.youtube.com/vi/'.$id.'/'.$img.'.jpg&quot</a>; alt="" class="'.$align_class.'" />'; } add_shortcode('youtube_thumb', 'wp_youtube_video_thumbnail'); 

Once done, you can use the shortcode. It accept 3 parameters: The video ID, the image size (0 for 480*360px, 1 for 120*90) and the image alignment.

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

Thanks to Gunay for the great code!

CodeRevolution Knowledge Base

Video tutorials