xpath("/rss/channel/item/media:thumbnail/@url"); $thumbnail = (string) $result[0]['url']; return $thumbnail; } // Justin.tv Functions function getJustintvInfo($id) { $xml = simplexml_load_file("http://api.justin.tv/api/clip/show/$id.xml"); return (string) $xml->clip->image_url_large; } // The Main Event function get_video_thumbnail($post_id=null) { // Get the post ID if none is provided if($post_id==null OR $post_id=='') $post_id = get_the_ID(); // Check to see if thumbnail has already been found if( ($thumbnail_meta = get_post_meta($post_id, '_video_thumbnail', true)) != '' ) { return $thumbnail_meta; } // If the thumbnail isn't stored in custom meta, fetch a thumbnail else { // Gets the post's content $post_array = get_post($post_id); $markup = $post_array->post_content; $markup = apply_filters('the_content',$markup); $new_thumbnail = null; // Simple Video Embedder Compatibility if(function_exists('p75HasVideo')) { if ( p75HasVideo($post_id) ) { $markup = p75GetVideo($post_id); } } // Checks for a standard YouTube embed preg_match('##s', $markup, $matches); // Checks for YouTube iframe if(!isset($matches[1])) { preg_match('#http://www.youtube.com/embed/([A-Za-z0-9\-_]+)#s', $markup, $matches); } // Checks for any YouTube URL if(!isset($matches[1])) { preg_match('#http://w?w?w?.?youtube.com/watch\?v=([A-Za-z0-9\-_]+)#s', $markup, $matches); } // If no standard YouTube embed is found, checks for one embedded with JR_embed if(!isset($matches[1])) { preg_match('#\[youtube id=([A-Za-z0-9\-_]+)]#s', $markup, $matches); } // If we've found a YouTube video ID, create the thumbnail URL if(isset($matches[1])) { $youtube_thumbnail = 'http://img.youtube.com/vi/' . $matches[1] . '/0.jpg'; // Check to make sure it's an actual thumbnail if (!function_exists('curl_init')) { $new_thumbnail = $youtube_thumbnail; } else { $ch = curl_init($youtube_thumbnail); curl_setopt($ch, CURLOPT_NOBODY, true); curl_exec($ch); $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // $retcode > 400 -> not found, $retcode = 200, found. curl_close($ch); if($retcode==200) { $new_thumbnail = $youtube_thumbnail; } } } // Vimeo if($new_thumbnail==null) { // Standard embed code preg_match('##s', $markup, $matches); // Find Vimeo embedded with iframe code if(!isset($matches[1])) { preg_match('#http://player.vimeo.com/video/([0-9]+)#s', $markup, $matches); } // If we still haven't found anything, check for Vimeo embedded with JR_embed if(!isset($matches[1])) { preg_match('#\[vimeo id=([A-Za-z0-9\-_]+)]#s', $markup, $matches); } // If we still haven't found anything, check for Vimeo URL if(!isset($matches[1])) { preg_match('#http://w?w?w?.?vimeo.com/([A-Za-z0-9\-_]+)#s', $markup, $matches); } // If we still haven't found anything, check for Vimeo shortcode if(!isset($matches[1])) { preg_match('#\[vimeo clip_id="([A-Za-z0-9\-_]+)"[^>]*]#s', $markup, $matches); } if(!isset($matches[1])) { preg_match('#\[vimeo video_id="([A-Za-z0-9\-_]+)"[^>]*]#s', $markup, $matches); } // Now if we've found a Vimeo ID, let's set the thumbnail URL if(isset($matches[1])) { $vimeo_thumbnail = getVimeoInfo($matches[1], $info = 'thumbnail_large'); if(isset($vimeo_thumbnail)) { $new_thumbnail = $vimeo_thumbnail; } } } // Blip.tv if($new_thumbnail==null) { // Blip.tv file URL preg_match('#http://blip.tv/play/([A-Za-z0-9]+)#s', $markup, $matches); // Now if we've found a Blip.tv file URL, let's set the thumbnail URL if(isset($matches[1])) { $blip_thumbnail = getBliptvInfo($matches[1]); $new_thumbnail = $blip_thumbnail; } } // Justin.tv if($new_thumbnail==null) { // Justin.tv archive ID preg_match('#archive_id=([0-9]+)#s', $markup, $matches); // Now if we've found a Justin.tv archive ID, let's set the thumbnail URL if(isset($matches[1])) { $justin_thumbnail = getJustintvInfo($matches[1]); $new_thumbnail = $justin_thumbnail; } } // Return the new thumbnail variable and update meta if one is found if($new_thumbnail!=null) { // Save as Attachment if enabled if(get_option('video_thumbnails_save_media')==1) { $ch = curl_init(); $timeout = 0; curl_setopt ($ch, CURLOPT_URL, $new_thumbnail); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $image_contents = curl_exec($ch); curl_close($ch); $upload = wp_upload_bits(basename($new_thumbnail), null, $image_contents); $new_thumbnail = $upload['url']; $filename = $upload['file']; $wp_filetype = wp_check_filetype(basename($filename), null ); $attachment = array( 'post_mime_type' => $wp_filetype['type'], 'post_title' => get_the_title($post_id), 'post_content' => '', 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment( $attachment, $filename, $post_id ); // you must first include the image.php file // for the function wp_generate_attachment_metadata() to work require_once(ABSPATH . "wp-admin" . '/includes/image.php'); $attach_data = wp_generate_attachment_metadata( $attach_id, $filename ); wp_update_attachment_metadata( $attach_id, $attach_data ); } // Add hidden custom field with thumbnail URL if(!update_post_meta($post_id, '_video_thumbnail', $new_thumbnail)) add_post_meta($post_id, '_video_thumbnail', $new_thumbnail, true); // Set attachment as featured image if enabled if(get_option('video_thumbnails_set_featured')==1 && get_option('video_thumbnails_save_media')==1 && get_post_meta($post_id, '_thumbnail_id', true) == '' ) { if(!update_post_meta($post_id, '_thumbnail_id', $attach_id)) add_post_meta($post_id, '_thumbnail_id', $attach_id, true); } } return $new_thumbnail; } }; // Echo thumbnail function video_thumbnail($post_id=null) { if( ( $video_thumbnail = get_video_thumbnail($post_id) ) == null ) { echo plugins_url() . "/video-thumbnails/default.jpg"; } else { echo $video_thumbnail; } }; // Create Meta Fields on Edit Page add_action("admin_init", "video_thumbnail_admin_init"); function video_thumbnail_admin_init(){ add_meta_box("video_thumbnail", "Video Thumbnail", "video_thumbnail_admin", "post", "side", "low"); } function video_thumbnail_admin(){ global $post; $custom = get_post_custom($post->ID); $video_thumbnail = $custom["_video_thumbnail"][0]; ?>