Do we all know about WordPress image sizes or WordPress thumbnails? Sometimes you may wish to delete WordPress image sizes from your website. Your website can be running on a premium WordPress theme and have a ton of plugins installed and active. Some plugins or themes may have registered a large number of image sizes to provide you with a variety of front-end layouts or functionalities.
However, as you begin to optimize your WordPress site, you find that some of these registered image sizes are unnecessary, and you want to get rid of them.
WordPress image sizes
WordPress creates different versions of any image you upload into your media library (WordPress post thumbnails or thumbnails for WordPress) and uses each of them throughout the front end and back end of your website.
The default image sizes that WordPress generates, are
- thumbnail (and its “thumb” alias)
- medium
- medium_large
- large
- full (the image you uploaded)
The sizes of these unique WordPress images might be rather large (5 or 10 additional image sizes, for example). Unregister the unused WordPress image sizes if you wish to optimize your hosting space and clean up your website.
- How to get a list of every WordPress Image Size registered?
- How to obtain the width, height, and cropping state of the registered image sizes?
- How to unregister the unused WordPress image sizes?
1. How to get a list of every WordPress Image Size registered?
In the functions.php file for your theme or any plugin-specific file, put the following code to get a list of all registered image sizes.
As soon as you’ve determined which registered image sizes are accessible, remove this code.
// get a list of all registered WordPress Images Sizes
$image_sizes = get_intermediate_image_sizes();
echo '<pre>';
print_r($image_sizes);
echo '</pre>';
The output of this code will look like the following:
It shows the list of 15 registered WordPress image sizes that are available.
2. How to obtain the width, height, and cropping state of the registered image sizes?
Advanced implementation. If you need to specify more details on registered images then follow the next steps.
In the functions.php file for your theme or any plugin-specific file, put the following code to get a detailed list of all registered image sizes.
As soon as you’ve determined which registered image sizes are accessible, remove this code.
// function to get all the image sizes
function blorax_get_image_sizes() {
global $_wp_additional_image_sizes;
$sizes = array();
foreach ( get_intermediate_image_sizes() as $_size ) {
if ( in_array( $_size, array('thumbnail', 'medium', 'medium_large', 'large') ) ) {
$sizes[ $_size ]['width'] = get_option( "{$_size}_size_w" );
$sizes[ $_size ]['height'] = get_option( "{$_size}_size_h" );
$sizes[ $_size ]['crop'] = (bool) get_option( "{$_size}_crop" );
} elseif ( isset( $_wp_additional_image_sizes[ $_size ] ) ) {
$sizes[ $_size ] = array(
'width' => $_wp_additional_image_sizes[ $_size ]['width'],
'height' => $_wp_additional_image_sizes[ $_size ]['height'],
'crop' => $_wp_additional_image_sizes[ $_size ]['crop'],
);
}
}
return $sizes;
}
$get_thumbnails = blorax_get_image_sizes();
// print out all the image sizes
echo '<pre>';
print_r($get_thumbnails);
echo '</pre>';
The output of this code will look like the following:
3. How to unregister the unused WordPress image sizes?
It is easy to unregister or remove unused image sizes if you identified already that these image sizes are not being used by your theme or are not required for your website.
In the functions.php file for your theme or any plugin-specific file, put the following code:
// Remove or unregister unused WordPress Image Sizes
function blorax_remove_intermediate_image_sizes($sizes, $metadata) {
$disabled_sizes = array(
'medium',
'medium_large',
'large',
'1536x1536',
'2048x2048',
'blog-large',
'blog-medium',
'widget-thumb-medium',
'widget-thumb'
);
// unset disabled sizes
foreach ($disabled_sizes as $size) {
if (!isset($sizes[$size])) {
continue;
}
unset($sizes[$size]);
}
return $sizes;
}
// Hook the function
add_filter('intermediate_image_sizes_advanced', 'blorax_remove_intermediate_image_sizes', 10, 2);
When a new image is uploaded to the media library, WordPress will generate automatically only image sizes not listed in our last code. Now, the website will use less disk space and bandwidth. For existing images use any plugin from the WordPress library to regenerate all images – Regenerate Thumbnails
Please note! Take a FULL Backup of your website before ANY optimization steps and TEST it.
We hope you find these code snippets useful!
Comments (2)
Great tutorial! it`s exactly what i was looking for. it helps me very easily delete unused images in wordpress
A better alternative for installing wordpress plugin to delete unused images… Thanks a lot