WordPress theme: force creation of newly created thumbnail size

I just added a new WordPress thumbnail size using add_image_size call, but now all the photos I already uploaded are not of the correct size. If I request them, all I get is the original image with width and height attributes correctly set.

How can I force the creation of that thumb, saving the data to the database as well, given an attachment ID?

Read More

Please note I want to do a simple admin operation or execute code from a theme file.

Related posts

Leave a Reply

2 comments

  1. I don’t know if there’s an operation to do this from the admin, but I can execute this code from a theme file:

    require(ABSPATH.'wp-admin/includes/image.php');
    $file = str_replace('http://www.example.com/', ABSPATH, $attachment->guid);
    wp_update_attachment_metadata( $attachment->ID,
        wp_generate_attachment_metadata( $attachment->ID, $file ) );
    

    that recreates all of the thumb sizes at once (don’t know if the already created ones are done as well, but it’s a one time code execution).

    You must replace “www.example.com” with your URL and $attachment with an actual attachment post structure, retrieved with get_post or similar.

  2. This is bug #13947. Instead of the_post_thumbnail() use this code in your theme until the bug is fixed:

    the_post_thumbnail(
        array ( 150, 150 ), // size
        array ( 'class' => 'attachment-post-thumbnail' )
    );