Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jul 27th, 2011  |  syntax: PHP  |  size: 1.86 KB  |  views: 913  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. 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.     $domain = parse_url( $url, PHP_URL_HOST );
  44.     $url = esc_url( $url );
  45.     if ( $domain == 'www.youtube.com' || $domain == 'youtube.com' ) {
  46.         parse_str( parse_url( $url, PHP_URL_QUERY ) );
  47.         $youtubeid = $v;
  48.     } else {
  49.         $youtubeid = '';
  50.     }        
  51.     return $youtubeid;
  52. }