Contact!

Regenerate WordPress media image sizes, programmatically

Regenerate WordPress media image sizes, programmatically

WordPress comes pre-packaged with several default image sizes which you can use on your site. When you upload an image, WordPress saves several ‘scaled’ versions of the media into your upload folder. As you can imagine, there are several reasons that you might want to regenerate your WordPress media and image sizes. Some common reasons include:

  • Generating thumbnails for a newly added image size.
  • Installing a new theme.
  • Updating and optimising image dimensions.

Here are a couple of approaches, although I advise using a plugin (or you could always use WP-CLI if that is more your boat).

Programatically regenerate media without a plugin

Let’s start programmatically regenerating our WordPress media with a function. We will need to pass it the ID of the image we want to update:

function gb_regenerate($imageId) {

}

The next step is to get the file path of the image using wp_get_original_image_path(). This function returns the file path of the original image (pre-sizing).

function gb_regenerate($imageId) {
    $imagePath = wp_get_original_image_path($imageId);
}

Now that we have the file path and the id, we can use it to regenerate the attachment using wp_generate_attachment_metadata(). As part of generating meta data for the attachment, this function also generates all of the media sizes (which is exactly what we are looking for). The caveat for using this is that wp_generate_attachment_metadata() is an admin function that we don’t normally have access to, therefore we also have to require the correct admin file.

Note: We should validate that we have a correct file path to pass to the function.

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

function gb_regenerate($imageId) {
    $imagePath = wp_get_original_image_path($imageId);

    // A sprinkle of validation
    if ($imagePath && file_exists($imagePath)) {
        wp_generate_attachment_metadata($imageId, $imagePath);
    }
}

So we have a function here which regenerates media, except you might have noticed that it still doesn’t work. The reason for this is simple; load order. Unfortunately we need certain parts of WordPress to have been loaded in order to get this to run. Time to refactor and find a hook to use!

The hook we are going to use is called ‘init’ which defers running the function until after WordPress has finished loading.

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

// Put the function in a class to make it more extendable
class GB_regen_media {
    public function gb_regenerate($imageId) {
        $imagePath = wp_get_original_image_path($imageId);

        if ($imagePath && file_exists($imagePath)) {
            wp_generate_attachment_metadata($imageId, $imagePath);
        }
    }
}

// Add a load function
function gb_regen_load() {
    // Instantiate the class
    $gb_regen_media = new GB_regen_media();

    // Set the image Id
    $imageId = 1;

    // You can get the image Id from the url of the media screen
    $gb_regen_media->gb_regenerate($imageId);
}

// Load after WordPress has finished loading
add_action('init', 'gb_regen_load');

Regenerate using a plugin

I don’t often recommend many plugins, but there are a handful out there which I consider very useful. One such plugin is Regenerate Thumbnails. This handy little plugin will regenerate WordPress media on your site so that you don’t have to replace/re-add it yourself.

Sadly the author of this plugin, Alex Mills, has passed away from Leukaemia. If you find this plugin useful then perhaps you might consider donating to Oregon Health & Science University in his memory.

Note: The plugin is open source and currently maintained by members of the WordPress community.

Join the discussion!

You might like: