2

I have a metabox that allows me to add videos from Youtube and Vimeo on posts with video post format. I am now looking into automatically generate thumbnails for these post with video post format. This is my meatabox bit of code that allows me to embed the videos:

function my_theme_show_post_video_metabox( $post ) {

  wp_nonce_field( 'my_theme_post_video_metabox', '_my_theme_post_video_metabox' );
  $video = get_post_meta( $post->ID, '_my_theme_post_video', true );

  echo '<h4 style="margin: 10px 0 0 0;">URL - YouTube or Vimeo</h4>';
  echo '<input type="text" id="_my_theme_post_video" name="_my_theme_post_video" value="' . $video . '" style="width: 100%;" />';

}

How could I now generate a thumbnail from the video embeded in this metabox for use as post featured image?

  • 1
    Use Youtube and Vimeo API to get thumbnails. Here is a related question stackoverflow.com/questions/1361149/… – Sisir Dec 31 '15 at 9:26
  • What does _my_theme_post_video data look like? How will you know it's YouTube vs. Vimeo? – jgraup Dec 31 '15 at 21:07
2

There are a few steps you need to cover.

  1. Get ID from MetaBox embed
  2. Pull list of available sizes based on ID
  3. Import image into your library if doesn't already exist
  4. Attach new file to post.

STEP 2 | YouTube

Generate an array of sizes from YouTube to use later:

function getYoutTubeImagesFromID($yt_id, $prefix = 'https:') {

    $yt_url = 'img.youtube.com'; //  can be 'i3.ytimg.com'

    $sizes = array(0, 1, 2, 3, "default", "hqdefault", "mqdefault", "sddefault", "maxresdefault");
    $result = array();
    foreach($sizes as $size) {
        $result [ $size ] = "{$prefix}//img.youtube.com/vi/{$yt_id}/{$size}.jpg";
    }

    return array(
        'id'    => $yt_id,
        'link'  => "{$prefix}//youtu.be/{$yt_id}",
        'sizes' => $result,
    );
}

To test this out, loop through a list of IDS.

$yt_videos = array('cV5yxrZpuXg', 'LtBTHqi8Gmw', 'VEAY-nRlP5c', 'IWeasYydL8Y', 'fCVXpcR9KDY');

foreach($yt_videos as $yt_id) {

    $ytImages = getYoutTubeImagesFromID($yt_id);

    $src = $ytImages[ 'sizes' ][ 'hqdefault' ];
    $link = $ytImages[ 'link' ];

    echo "<a href=\"{$link}\" target=\"_blank\" ><img src=\"{$src}\" ></a>";

    print_r($ytImages);
}

They should each produce size information you can use for your import.

Array
(
    [id] => cV5yxrZpuXg
    [link] => https://youtu.be/cV5yxrZpuXg
    [sizes] => Array
        (
            [0] => https://img.youtube.com/vi/cV5yxrZpuXg/0.jpg
            [1] => https://img.youtube.com/vi/cV5yxrZpuXg/1.jpg
            [2] => https://img.youtube.com/vi/cV5yxrZpuXg/2.jpg
            [3] => https://img.youtube.com/vi/cV5yxrZpuXg/3.jpg
            [default] => https://img.youtube.com/vi/cV5yxrZpuXg/default.jpg
            [hqdefault] => https://img.youtube.com/vi/cV5yxrZpuXg/hqdefault.jpg
            [mqdefault] => https://img.youtube.com/vi/cV5yxrZpuXg/mqdefault.jpg
            [sddefault] => https://img.youtube.com/vi/cV5yxrZpuXg/sddefault.jpg
            [maxresdefault] => https://img.youtube.com/vi/cV5yxrZpuXg/maxresdefault.jpg
        )

)

STEP 2 | Vimeo

Gather Vimeo video information

function getVimeoImagesFromID($vimeo_id, $prefix = 'https:') {

    $vimeo_url = "{$prefix}//vimeo.com/api/v2/video/{$vimeo_id}.php";

    if( ! $vimeo_hash = @file_get_contents($vimeo_url)) {
        return false;
    }

    $result = unserialize($vimeo_hash);

    return ( count($result) === 1 ) ? array_shift($result) : $result;
}

// your vimeo id 
$vimeo_id = 71472926;

// get available information for video
if($vimeo_hash = getVimeoImagesFromID($vimeo_id)) {

    // grab the largest image possible
    $vimeo_src = $vimeo_hash [ 'thumbnail_large' ];

    echo $vimeo_src; // https://i.vimeocdn.com/video/445043557_640.jpg
}

STEP 3 | Sideload Image

media_sideload_image the image from YT to your sever. Be sure to grab the thumbnail_id from the resulting src.

// your youtube id
$yt_id = 'cSXlOeKusWs';

// size information for id
$ytImages = getYoutTubeImagesFromID($yt_id);

// max res image
$url = $ytImages[ 'sizes' ][ 'maxresdefault' ];

// sideload image
$src = media_sideload_image(esc_url($url), NULL, '', 'src');

// convert src to id
$thumbnail_id = attachment_url_to_postid($src);

STEP 4 | Attach Post Thumbnail

set_post_thumbnail based on the new thumbnail_id.

set_post_thumbnail( $post, $thumbnail_id );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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