This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jun 24th, 2011  |  syntax: PHP  |  size: 1.83 KB  |  views: 276  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // Save Featured Image If It's a youtube video
  2. add_action('save_post', 'wds_video_sideload_post_thumb');
  3. function wds_video_sideload_post_thumb() {
  4. global $post;
  5.  
  6.     $youtube_url = get_post_meta( $post->ID, 'videobox', true );
  7.     $youtubeid = youtubeid($youtube_url);
  8.         $thumb_url = 'http://img.youtube.com/vi/'. $youtubeid .'/0.jpg';
  9.            
  10.         require_once(ABSPATH . 'wp-admin/includes/file.php');
  11.         require_once(ABSPATH . 'wp-admin/includes/media.php');
  12.         set_time_limit(300);
  13.  
  14.         if ( ! empty($thumb_url) ) {
  15.             // Download file to temp location
  16.             $tmp = download_url( $thumb_url );
  17.  
  18.             // Set variables for storage
  19.             // fix file filename for query strings
  20.             preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $thumb_url, $matches);
  21.             $file_array['name'] = basename($matches[0]);
  22.             $file_array['tmp_name'] = $tmp;
  23.  
  24.             // If error storing temporarily, unlink
  25.             if ( is_wp_error( $tmp ) ) {
  26.                 @unlink($file_array['tmp_name']);
  27.                 $file_array['tmp_name'] = '';
  28.             }
  29.  
  30.             // do the validation and storage stuff
  31.             $thumbid = media_handle_sideload( $file_array, $post->ID, $desc );
  32.             // If error storing permanently, unlink
  33.             if ( is_wp_error($thumbid) ) {
  34.                 @unlink($file_array['tmp_name']);
  35.                 return $thumbid;
  36.             }
  37.         }
  38.  
  39.         set_post_thumbnail( $post, $thumbid );
  40. }
  41.  
  42. function youtubeid($url) {
  43.         if (preg_match('%youtube\\.com/(.+)%', $url, $match)) {
  44.                 $match = $match[1];
  45.                 $replace = array("watch?v=", "v/", "vi/");
  46.                 $match = str_replace($replace, "", $match);
  47.                 $match = substr( $match, 0, ( strpos( $match, '&')  ) );
  48.         }
  49.         return $match;
  50. }
clone this paste RAW Paste Data