Advertisement
ggsalas

Save External image as Featured with WPUF plugin

Sep 11th, 2013
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.48 KB | None | 0 0
  1. // Save External image as Featured with WPUF plugin
  2. add_action('wpuf_add_post_after_insert', 'featured_img_wpuf');
  3. function featured_img_wpuf($post_id) {
  4.  
  5.     //$enlace_link = get_post_meta( $post_id, 'url_link', true );
  6.     $thumb_url = 'http://img.vidaextra.com/2012/11/031112_imagen-disney_01.jpg';
  7.            
  8.         require_once(ABSPATH . 'wp-admin/includes/file.php');
  9.         require_once(ABSPATH . 'wp-admin/includes/media.php');
  10.         set_time_limit(300);
  11.  
  12.         if ( ! empty($thumb_url) ) {
  13.             // Download file to temp location
  14.             $tmp = download_url( $thumb_url );
  15.  
  16.             // Set variables for storage
  17.             // fix file filename for query strings
  18.             preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $thumb_url, $matches);
  19.             $file_array['name'] = basename($matches[0]);
  20.             $file_array['tmp_name'] = $tmp;
  21.  
  22.             // If error storing temporarily, unlink
  23.             if ( is_wp_error( $tmp ) ) {
  24.                 @unlink($file_array['tmp_name']);
  25.                 $file_array['tmp_name'] = '';
  26.             }
  27.  
  28.             // do the validation and storage stuff
  29.             $thumbid = media_handle_sideload( $file_array, $post_id, $desc );
  30.             // If error storing permanently, unlink
  31.             if ( is_wp_error($thumbid) ) {
  32.                 @unlink($file_array['tmp_name']);
  33.                 return $thumbid;
  34.             }
  35.         }
  36.  
  37.         set_post_thumbnail( $post_id, $thumbid );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement