WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have recently added theme support for post_thumbnails with three new sizes, I wanted to resize all the old images uploaded previously. I have written my own script as follows:

function resizeImages()

    require ( ABSPATH . 'wp-admin/includes/image.php' );

    global $wpdb;
    $images = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND post_mime_type LIKE 'image/%' ORDER BY ID DESC");

    foreach ($images as $image)
        $fullsizepath = get_attached_file( $image->ID );

        $metadata = wp_generate_attachment_metadata( $image->ID, $fullsizepath );
        // If this fails, then it just means that nothing was changed (old value == new value)
        if (wp_update_attachment_metadata( $image->ID, $metadata ))
                    echo $fullsizepath . " resized" . "<br/>";


The images that have been uploaded through wordpress work fine, but I have added some of my images by php. The images in question were picked up from our old site with a crawler and added to the database as children of the correct ID. I can retrieve all the images using get_children on the index.php page and on single.php, but for some reason they are not being resized. Can anyone help here?

share|improve this question
Close-vote for being too localized, as essentially a "debug my code" type of question. Also, downvote for failing to follow up on/respond to answers posted. – Chip Bennett Feb 15 '12 at 16:32

use this plugin instead. Always work for me :) http://wordpress.org/extend/plugins/regenerate-thumbnails

share|improve this answer
Tried it, didn't work because problem was images had no wp_metadata, as is flagged on the plugin page, so I coded my own class to take all the images from the wp_posts db, search and find them on the filesystem to get their full path, then used wp_insert_attachment, wp_generate_metadata, and wp_update_metadata to achieve the goal. – Liam Bailey May 6 '11 at 19:41

Try using the Add From Server Plugin, to ensure that your PHP-added images have been added properly to the database.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.