Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

If I create a post that has an embedded YouTube video in it (so all I do is paste the YouTube URL into the post and let Wordpress automatically embed it for me), is there a way to have the thumbnail image of the video set as the post's featured image?

share|improve this question
add comment

2 Answers

up vote 11 down vote accepted

Not natively. You'd have to write some code to make it happen - there's a nice pastebin function that provide the necessary code to do it.

Edit (12/19/2011):

Yep here's how you can do this programmatically. Add the following two functions to your functions.php file and you should be good to go. The code has been commented to explain what's happening, but here's a high level of what to expect:

You must...

  • Create a post
  • In the content, include a YouTube URL

The code will...

  • Parse the URL's out of the content
  • Will grab the first URL that it finds and assume it's a YouTube URL
  • Grab the thumbnail from the remote server and download it
  • Set it as the current post's thumbnail

Note that if you include multiple URLs in your post, you'll need to modify the code to properly find the YouTube URL. This can be done by iterating through the $attachments collection and sniffing out what URL's look like a YouTube URL.

function set_youtube_as_featured_image($post_id) {  

    // only want to do this if the post has no thumbnail
    if(!has_post_thumbnail($post_id)) { 

        // find the youtube url
        $post_array = get_post($post_id, ARRAY_A);
        $content = $post_array['post_content'];
        $youtube_id = get_youtube_id($content);

        // build the thumbnail string
        $youtube_thumb_url = 'http://img.youtube.com/vi/' . $youtube_id . '/0.jpg';

        // next, download the URL of the youtube image
        media_sideload_image($youtube_thumb_url, $post_id, 'Sample youtube image.');

        // find the most recent attachment for the given post
        $attachments = get_posts(
            array(
                'post_type' => 'attachment',
                'numberposts' => 1,
                'order' => 'ASC',
                'post_parent' => $post_id
            )
        );
        $attachment = $attachments[0];

        // and set it as the post thumbnail
        set_post_thumbnail( $post_id, $attachment->ID );

    } // end if

} // set_youtube_as_featured_image
add_action('save_post', 'set_youtube_as_featured_image');

function get_youtube_id($content) {

    // find the youtube-based URL in the post
    $urls = array();
    preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $content, $urls);
    $youtube_url = $urls[0][0];

    // next, locate the youtube video id
    $youtube_id = '';
    if(strlen(trim($youtube_url)) > 0) {
        parse_str( parse_url( $youtube_url, PHP_URL_QUERY ) );
        $youtube_id = $v;
    } // end if

    return $youtube_id; 

} // end get_youtube_id

One thing to note is that this assumes that your post has no post thumbnail and will not fire once a post thumbnail is set.

Secondly, if you remove the post thumbnail and then attach an image to this post using the media uploader, the most recent image will be used.

share|improve this answer
    
Great snippet, but since who knows if it will be there year from now - please try to include at least summary (if full code is too much) of what it does in your answer (like "use this to get thumbnail, then that to download and attach"). –  Rarst Dec 13 '11 at 11:05
add comment

The simplest way is to us Video Thumbnails plugin for wordpress

share|improve this answer
    
Lone link is considered a poor answer since it is meaningless by itself and target resource is not guaranteed to be alive in the future. Please try to include at least summary of information you are linking to. –  Rarst Apr 17 '12 at 12:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.