Advertisement
Guest User

Untitled

a guest
Apr 29th, 2013
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.06 KB | None | 0 0
  1.  
  2.                         $auto_new_post = array(
  3.                             'post_title' => $img_title,  
  4.                             'comment_status' => 'open',
  5.                             'post_name' => $slug,
  6.                             'post_content' => '',
  7.                             'post_status' => 'draft',
  8.                             'post_date' => date('Y-m-d H:i:s'),
  9.                             'post_author' => 2,
  10.                             'post_type' => 'post',
  11.                             'post_category' => $cat_ids
  12.                         );
  13.  
  14.                         $auto_post_id = wp_insert_post($auto_new_post);
  15.  
  16.                         require_once(ABSPATH . 'wp-admin/includes/file.php');
  17.                         require_once(ABSPATH . 'wp-admin/includes/media.php');
  18.                         require_once(ABSPATH . 'wp-admin/includes/image.php');
  19.  
  20.                         $media_img_url = 'http://localhost/'.$save_path ;
  21.                         $tmp = download_url( $media_img_url );
  22.                         $post_id = $auto_post_id ;
  23.                         $desc = $img_title;
  24.  
  25.                         // Set variables for storage
  26.                         // fix file filename for query strings
  27.                         preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $media_img_url, $matches);             
  28.                         $file_array['name'] = basename($matches[0]);
  29.                         $file_array['tmp_name'] = $tmp;
  30.  
  31.                         // If error storing temporarily, unlink
  32.                         if ( is_wp_error( $tmp ) ) {
  33.                             @unlink($file_array['tmp_name']);
  34.                             $file_array['tmp_name'] = '';
  35.                         }
  36.  
  37.                         // do the validation and storage stuff
  38.                         $id = media_handle_sideload( $file_array, $post_id, $desc );
  39.  
  40.                         // If error storing permanently, unlink
  41.                         if ( is_wp_error($id) ) {
  42.                             @unlink($file_array['tmp_name']);
  43.                             return $id;
  44.                         }
  45.                         if(is_readable($save_path)) { unlink($save_path); }
  46.  
  47.                         $src = wp_get_attachment_url( $id );
  48.  
  49.                         // set image as the post thumbnail
  50.                         set_post_thumbnail($post_id, $id);
  51.  
  52.                         $content = '<img src="'.$src.'" alt="'.$img_title.'" title="'.$img_title.'" class="alignnone size-full wp-image-'.$id.' width="500"/>';
  53.  
  54.                         // Update post
  55.                         $new_auto_post = array();
  56.                         $new_auto_post['ID'] = $auto_post_id;
  57.                         $new_auto_post['post_content'] = $content;
  58.  
  59.                         // Update the post into the database
  60.                         wp_update_post( $new_auto_post );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement