DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

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="<a href="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!

12 Comments

  1. That’s a nice snippet, thanks!

  2. yes great ! and for vimeo ? something like this ? http://snipt.net/styledev/vimeo-thumbnail/

  3. doesn’t seem to work

  4. This trick will be helpful to reduce page load time.

  5. Looks cool, but how can I use the Image as WordPress Post Thumbnail?! Any Idea to get this working?

  6. Coool! But should be

    return ”;

    Also img=0,1,2 or 3 is not the size , it’s rather a thumbnail id in the set of thumbnails for that video

  7. I couldn’t add code in the comment. I just wanted to say that “return” should be fixed

  8. The snippet didn’t work for me either.

  9. Is there a way to auto post youtube videos into categories with thumbnails, so i can share the posted videos from my website instead of sharing them from youtube?

    I tried FeedWordPress and youtube posts, i tried thumbnails generator as well…nothing see to work properly, youtube posts works fine but does not display a thumbnail.

    any help?

  10. Please, this is not working like others have expressed. Could you provide and update on how to get this done? Thanks

  11. To make it work, just replace the return string on it:

    return '';

  12. errrr

    Hey guys

    you have to modify “return” because the “a” is in the “img”. Just put the “a” around the “img” and modify the value of the link.
    The value of the “a” should be the link to the video and not a img link.

    ++

Leave a Comment

Your email address will not be published. Required fields are marked *