Using WordPress ‘intermediate_image_sizes’ PHP filter

The intermediate_image_sizes WordPress PHP Filter allows you to modify the list of intermediate image sizes.

Usage

add_filter('intermediate_image_sizes', 'my_custom_intermediate_sizes');
function my_custom_intermediate_sizes($default_sizes) {
    // your custom code here
    return $default_sizes;
}

Parameters

  • $default_sizes (string[]): An array of intermediate image size names. Defaults are ‘thumbnail’, ‘medium’, ‘medium_large’, ‘large’.

More information

See WordPress Developer Resources: intermediate_image_sizes

Examples

Add Custom Image Size

Add a custom image size called ‘custom_size’ to the list of intermediate image sizes.

add_filter('intermediate_image_sizes', 'add_custom_size');
function add_custom_size($default_sizes) {
    $default_sizes[] = 'custom_size';
    return $default_sizes;
}

Remove Large Image Size

Remove the ‘large’ image size from the list of intermediate image sizes.

add_filter('intermediate_image_sizes', 'remove_large_size');
function remove_large_size($default_sizes) {
    $key = array_search('large', $default_sizes);
    if (false !== $key) {
        unset($default_sizes[$key]);
    }
    return $default_sizes;
}

Keep Only Thumbnail Image Size

Keep only the ‘thumbnail’ image size and remove all other intermediate image sizes.

add_filter('intermediate_image_sizes', 'keep_thumbnail_only');
function keep_thumbnail_only($default_sizes) {
    return array('thumbnail');
}

Reorder Image Sizes

Reorder the list of intermediate image sizes.

add_filter('intermediate_image_sizes', 'reorder_sizes');
function reorder_sizes($default_sizes) {
    return array('large', 'medium', 'thumbnail', 'medium_large');
}

Rename Medium Image Size

Rename the ‘medium’ image size to ‘custom_medium’.

add_filter('intermediate_image_sizes', 'rename_medium_size');
function rename_medium_size($default_sizes) {
    $key = array_search('medium', $default_sizes);
    if (false !== $key) {
        $default_sizes[$key] = 'custom_medium';
    }
    return $default_sizes;
}