Skip to content

Instantly share code, notes, and snippets.

@retlehs
Created May 25, 2012 02:17
Show Gist options
  • Save retlehs/2785401 to your computer and use it in GitHub Desktop.
Save retlehs/2785401 to your computer and use it in GitHub Desktop.
Display YouTube thumbnail from embed URL
<?php
/**
* Display a thumbnail from YouTube based off the embed code saved in the
* video post format metabox used by the CF Post Formats plugin
*
* @link https://github.com/crowdfavorite/wp-post-formats
* @link http://stackoverflow.com/a/6382259
*/
global $post;
$video_details = get_post_meta($post->ID, '_format_video_embed', true);
if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $video_details, $match)) {
$video_id = $match[1];
}
if (isset($video_id)) { ?>
<a href="<?php the_permalink(); ?>"><img src="http://img.youtube.com/vi/<?php echo $video_id; ?>/0.jpg"></a>
<?php }
?>
@alexkingorg
Copy link

Perhaps even cooler to sideload the image and set it as the featured image?

@dingman
Copy link

dingman commented Jul 3, 2012

@alex - I have a media_sideload_image() built into my plugin/function. Feel free to take a look. It sets it as the featured image too. Works for Youtube, WordPress.TV, and Wistia embeds.

http://wpforce.com/download-video-thumbnail-set-featured-image/

@alexkingorg
Copy link

@dingman I'd love to check out a pull request...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment