How to set youtube thumbnail from youtube embed code as featured image in wordpress?

Polo Pakina picture Polo Pakina · Jul 20, 2013 · Viewed 39.2k times · Source

How to set external url of youtube thumbnail (not uploading it inside my site) of an youtube video from iframe embed code of youtube video?

Is it possible to make function which will pull external youtube thumbnail URL and set it as featured image from:

<iframe width="420" height="315" src="www.youtube.com/embed/gTp4sZtjHS" frameborder="0" allowfullscreen></iframe>

Do not ask about OEmbed or similar things, I think question is clear.

Answer

phil picture phil · Jul 20, 2013

The format for pulling the large image from a YouTube video is this http://img.youtube.com/vi/A4a0xZMMlqE/0.jpg just replace "A4a0xZMMlqE" with the video id.

I think the easiest way to set this up would be to forget about using it as a featured image but just pulling it into the template dynamically.

So in your post, make a custom field that will hold the YouTube video ID and in the template do something like:

<img src="http://img.youtube.com/vi/<?php echo get_post_meta($post->ID,'youtubeId');?>/0.jpg"/>

Does that solve your problem?

Otherwise if you're looking at really making it the featured image then you're probably looking at developing a custom plugin: http://net.tutsplus.com/tutorials/wordpress/creating-a-custom-wordpress-plugin-from-scratch/ which will most likely make use of wp_insert_attachment

I also just found media_sideload_image which I wasn't aware of. It will pull an image from a URL (i.e. the youtube link above) and attach it to a post.