WordPress.org

Ready to get started?Download WordPress

Forums

wp_update_attachment_metadata not creating the other image sizes (5 posts)

  1. vasilescu_anton
    Member
    Posted 2 years ago #

    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!

    Thank you,
    Anton

  2. vasilescu_anton
    Member
    Posted 2 years ago #

    The image is already uploaded to the server by the way!

  3. thereformation
    Member
    Posted 2 years ago #

    Have a look at the regenerate thumbnails plugin. Seems to do basically what you are after.

  4. vasilescu_anton
    Member
    Posted 2 years ago #

    I figured it out why it wasn't working. I was feeding the url to the file instead of the absolute path.

  5. paul-r
    Member
    Posted 1 year ago #

    Thanks Anton
    I was making the same mistake and took me hours to figure it out what went wrong. Great that you shared the answer on the forum!

Topic Closed

This topic has been closed to new replies.

About this Topic