youthumb (by YCB)

youtube thumbnailerYouThumb is a WordPress plugin that extends the functionality of a previous thumbnail generator. It automatically adds a thumbnail of the first YouTube video embedded in the post.

    Features:

  • It automatically adds an “entry-preview” custom field to posts that are published and have a youtube embed code, and it fills it with a link to the thumbnail.
    Future Work:

  • Seamlessly integrate with the MS auto-thumbnail-generator plugin to allow for different key titles than “entry-preview”.
<?php
/*
Plugin Name: YouThumb
Plugin URI: http://gurukhalsa.me/youthumb
Description: Automatically detects youtube embeds in posts before they are added, and sets the "entry-preview" field to link to a thumbnail of it.
Author: Guru Khalsa
Author URI: http://gurukhalsa.me
*/
if (!class_exists("YouThumb")) {
class YouThumb {

private $flag = False;
private $ytid = “”;
private $yt_thumb = “”;
private $uploaded_url=”";

function YouThumb() { //constructor

}

function detectEmbed($content){
$matches;
#gets the first youtube ID it sees that follows the pattern …/v/(youtube ID)&…
if(preg_match(‘#/v/(.+?)&#’, $content, $matches)){
#sets ytid to the matched id, and sets the flag to true
$this->ytid = $matches[1];
$this->flag = True;
$this->getYtThumb();
}

return $content;
}

function uploadThumbnail($post_ID){
#uploads and returns <img src=”blah/blah/blah/the_new_url.jpg” />
$yt_thumbnail_html = media_sideload_image($this->yt_thumb,$post_ID);

#get just the URL
$matches;
if(preg_match(“#src=’(.+?)’#”, $yt_thumbnail_html, $matches)){
$this->uploaded_url = $matches[1];
}
}

function getYtThumb(){
$this->yt_thumb = “http://img.youtube.com/vi/”.$this->ytid.”/2.jpg”;
}

function addThumbnail($post_ID){
if(get_post_meta($post_ID,’entry-preview’,true)==”"){
if($this->flag){
$this->uploadThumbnail($post_ID);

update_post_meta($post_ID, ‘entry-preview’, $this->uploaded_url);
}
}

}

}

} //End Class YouThumb

if (class_exists(“YouThumb”)) {
$ns_youthumb = new YouThumb();
}

//Actions and Filters
if (isset($ns_youthumb)) {
add_filter(‘content_save_pre’, array(&$ns_youthumb, ‘detectEmbed’));

add_action(‘publish_post’, array(&$ns_youthumb, ‘addThumbnail’));
}

?>

Download here.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>