I have sourced the following code which captures the URL for a YouTube video placed in the content/body the code then gets the video image from YouTube and sets the image as a featured image.

    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(
                '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

However instead of placing the YouTube URL in the body I'm using a custom field to store the URL. Which can be accessed via the following code

$vidlink = get_post_meta($post->ID, 'ci_cpt_video_link', true);

How can I modify the initial code to use the YouTube URL stored in the custom field to get the video image from YouTube so it can be set as the featured image?

2 Answers 2

function set_youtube_as_featured_image($post_id) { 

  if(  has_post_thumbnail($post_id) ) return;

  $url = get_post_meta($post_id, 'ci_cpt_video_link', true);

  if ( $url && filter_var($url, FILTER_VALIDATE_URL) ) {

    // getting thumb url from video url
    parse_str( parse_url( $url, PHP_URL_QUERY ), $youtube_vars );
    $youtube_id = $youtube_vars['v'];
    $youtube_thumb_url = 'http://img.youtube.com/vi/' . $youtube_id . '/0.jpg';

    // download and save thumb
    $get = wp_remote_get( $youtube_thumb_url );
    $mime_type = wp_remote_retrieve_header( $get, 'content-type' );
    if ( ! substr_count($mime_type, 'image') ) return false;
    $name = 'youtube-thumb-post-' . $post_id . '.jpg';
    $bits = wp_upload_bits( $name, '', wp_remote_retrieve_body( $get ) );
    if ( $bits['error'] ) return false;

    // save attachment post, and setting as post thumbnails
    $thumb_data = array(
      'post_title'=> 'Youtube Preview', 'post_mime_type' => $mime_type
    $thumbnail_id  = wp_insert_attachment( $thumb_data, $bits['file'], $post_id );
    if ( $thumbnail_id ) {
      require_once(ABSPATH . 'wp-admin/includes/image.php');
      $metadata = wp_generate_attachment_metadata( $thumbnail_id, $bits['file'] );
      wp_update_attachment_metadata( $thumbnail_id, $metadata );
      set_post_thumbnail( $post, $thumbnail_id );


add_action('save_post', 'set_youtube_as_featured_image');
  • You are welcome. I adjust get_post_meta() to use $post_id instead $post->ID: you don't need globalize post once you have $post_id passed as param to function.
    – gmazzap
    Jul 26, 2013 at 5:18

The easiest way to achieve this is using the "Video Thumbnails" plugin.

Just activate it and in the settings area, specify the custom field your storing your YouTube URL in.

It saves the image to the WordPress media library when you publish your post, and it can scan and generate thumbnails for post published prior to installing the plugin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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