Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $auto_new_post = array(
- 'post_title' => $img_title,
- 'comment_status' => 'open',
- 'post_name' => $slug,
- 'post_content' => '',
- 'post_status' => 'draft',
- 'post_date' => date('Y-m-d H:i:s'),
- 'post_author' => 2,
- 'post_type' => 'post',
- 'post_category' => $cat_ids
- );
- $auto_post_id = wp_insert_post($auto_new_post);
- require_once(ABSPATH . 'wp-admin/includes/file.php');
- require_once(ABSPATH . 'wp-admin/includes/media.php');
- require_once(ABSPATH . 'wp-admin/includes/image.php');
- $media_img_url = 'http://localhost/'.$save_path ;
- $tmp = download_url( $media_img_url );
- $post_id = $auto_post_id ;
- $desc = $img_title;
- // Set variables for storage
- // fix file filename for query strings
- preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $media_img_url, $matches);
- $file_array['name'] = basename($matches[0]);
- $file_array['tmp_name'] = $tmp;
- // If error storing temporarily, unlink
- if ( is_wp_error( $tmp ) ) {
- @unlink($file_array['tmp_name']);
- $file_array['tmp_name'] = '';
- }
- // do the validation and storage stuff
- $id = media_handle_sideload( $file_array, $post_id, $desc );
- // If error storing permanently, unlink
- if ( is_wp_error($id) ) {
- @unlink($file_array['tmp_name']);
- return $id;
- }
- if(is_readable($save_path)) { unlink($save_path); }
- $src = wp_get_attachment_url( $id );
- // set image as the post thumbnail
- set_post_thumbnail($post_id, $id);
- $content = '<img src="'.$src.'" alt="'.$img_title.'" title="'.$img_title.'" class="alignnone size-full wp-image-'.$id.' width="500"/>';
- // Update post
- $new_auto_post = array();
- $new_auto_post['ID'] = $auto_post_id;
- $new_auto_post['post_content'] = $content;
- // Update the post into the database
- wp_update_post( $new_auto_post );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement