Skip to content

Instantly share code, notes, and snippets.

@TheHeat
Created March 30, 2013 09:04
Show Gist options
  • Save TheHeat/5276001 to your computer and use it in GitHub Desktop.
Save TheHeat/5276001 to your computer and use it in GitHub Desktop.
In WordPress, if a featured image isn't set. check for a custom field called 'video' and if there's a url in there from either Vimeo or YouTube (including youtu.be shortened links) nab the thumbnail.
<?php
// Check for a featured image with get_the_image by Justin Tadlcok http://themehybrid.com
// if one is not found check for a custom field called video
// if there is a custom field called video and it has an address from Vimeo or YouTube, grab the thumbnail
?>
<?php $thid = get_the_ID();
$vim_prefix = 'http://vimeo.com/';
$yt_prefix = 'http://www.youtube.com/watch?v=';
$yts_prefix = 'http://youtu.be/';
$str = get_post_meta( $thid, 'video', true );
if (substr($str, 0, strlen($vim_prefix)) == $vim_prefix) {
$str = substr($str, strlen($vim_prefix), strlen($str));
$imgid = $str;
$hash = unserialize(file_get_contents("http://vimeo.com/api/v2/video/$imgid.php"));
$default_image = $hash[0]['thumbnail_large'];
}
elseif(substr($str, 0, strlen($yt_prefix)) == $yt_prefix) {
$str = substr($str, strlen($yt_prefix), strlen($str));
$imgid = $str;
$default_image = 'http://img.youtube.com/vi/' . $str . '/0.jpg';
}
elseif(substr($str, 0, strlen($yts_prefix)) == $yts_prefix) {
$str = substr($str, strlen($yts_prefix), strlen($str));
$imgid = $str;
$default_image = 'http://img.youtube.com/vi/' . $str . '/mqdefault.jpg';
}
if(function_exists('get_the_image')){
get_the_image(array('default_image'=>"$default_image"));
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment