How to extract YouTube Thumb Image URL from a YouTube Video URL using PHP Script

by KishPress on September 22, 2010

If you have thumbnails for your blog theme and you have no images in the post, but you have YouTube Videos, then you can pull the YouTube Video Thumbnail using this PHP function.

function kish_update_video_thumbnail($youtubeurl, $postid) {
	if(strlen($youtubeurl)) {
		//$pattern="/[\?&]v=([^&#]*)/";
		$pattern="/http://www.youtube.com/v/([w-]{11})/";
		preg_match($pattern, $youtubeurl, $matches);
		$thumb="http://img.youtube.com/vi/".$matches[1]."/0.jpg";
		update_post_meta($postid, 'video_thumb', $thumb);
	}
}

The commented pattern can be used if you are using the video URL. This will update the post_meta with the thumb URL

Related posts:

  1. How to extract the title and meta description using Simple HTML Dom Parser Simple HTML Dom is an excellent script in PHP to...
  2. Installing Kish Twit Pro – Quick Guide with Video Installation of Kish Twit Pro is very simple like a...
  3. How to do a post using Kish Multi Pro? Writing a blog post is made very simple when you...
  4. Updating post custom fields using XMLRPC – WordPress Updating the custom data of a post which can be...
  5. WordPress has enabled Phone Blogging   We have been blogging using Windows Live Writer, Ecto,...

{ 4 comments… read them below or add one }

Leon October 16, 2010 at 2:18 pm

Thanks for the code…How would you then call this function to display the thumb in a portfolio page?

Reply

KishPress October 16, 2010 at 8:24 pm

You can call this function where ever you want and have an YouTube URL

Reply

Jocuri February 14, 2011 at 1:04 pm

Great code. but i have a question, do you can show or maybe suggest how can i save the thumb into o folder on my website?

Reply

KishPress February 14, 2011 at 4:54 pm

You can use the copy function to do that http://php.net/manual/en/function.copy.php

Reply

Leave a Comment

Previous post:

Next post: