I am trying to update an image attachment post and I've finally got it working to replace the image but wp_update_attachment_metadata does not create the thumbnails and the other media sizes. I've looked all over for a possible answer but to no avail.
Here a simplified version of the code that I am using which is basically the one provided in the wp_insert_attachment codex page:
$filename = 'http://www.mysite.com/wp-content/uploads/2012/01/Earth-Western-Hemisphere.jpg'; $wp_filetype = wp_check_filetype(basename($filename), null ); $attachment = array( 'ID' => 705, 'post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)), 'post_content' => '', 'post_excerpt' => '', 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment( $attachment, $filename, 704 ); // you must first include the image.php file // for the function wp_generate_attachment_metadata() to work require_once(ABSPATH . 'wp-admin/includes/image.php'); //update_attached_file( $attach_id, $filename ); $attach_data = wp_generate_attachment_metadata( $attach_id, $filename ); wp_update_attachment_metadata( $attach_id, $attach_data );
Any help would be greatly appreciated!