How to Disable All Unwanted Image Sizes
This year, give your site the gift that keeps on giving: better optimized image handling. As of version 5.3, WordPress automatically generates seven (7!) additional images for every image that you upload via the WP Media Library (and/or RTE/Visual editor). So in addition to the original image on the server, WordPress generates images with the following dimensions:
Image Size | Dimensions |
---|---|
Thumbnail | (Size based on Media settings) |
Medium | (Size based on Media settings) |
Large | (Size based on Media settings) |
Medium Large | 768px |
2x Medium Large | 1536px |
2x Large | 2048px |
Scaled | 2560px |
Here is an example of what all those extra images look like on the server. By default, for each and every uploaded image you get this:
What does this mean for you and your website? It means that all those extra images are eating up precious server space and resources. If you make use of all the alternate images, then great; stop reading here and go enjoy the holidays. Otherwise, you may want to optimize WordPress default image handling and disable any unwanted image sizes. You know, to save disk space and keep your site running lean, mean, and wonderful.
How to disable unwanted image sizes
Fortunately, WordPress provides a way to do almost anything, including disabling all of the extra image sizes. So for this year’s “Snippets til Christmas” post, I’m going to share the magic recipe to give you full control over all WordPress generated images. Ready? Here goes:
// disable generated image sizes
function shapeSpace_disable_image_sizes($sizes) {
unset($sizes['thumbnail']); // disable thumbnail size
unset($sizes['medium']); // disable medium size
unset($sizes['large']); // disable large size
unset($sizes['medium_large']); // disable medium-large size
unset($sizes['1536x1536']); // disable 2x medium-large size
unset($sizes['2048x2048']); // disable 2x large size
return $sizes;
}
add_action('intermediate_image_sizes_advanced', 'shapeSpace_disable_image_sizes');
That takes care of all but the “Scaled” image size, which can be handled by adding the following line:
// disable scaled image size
add_filter('big_image_size_threshold', '__return_false');
Add those two code snippets to your theme’s functions.php
file and done. Once added, the code tells WordPress to NOT generate any extra images. So only the original uploaded image will be added to the server. And if you want to keep some of the generated image sizes, you can simply remove the corresponding line from the function. For example, if I want WordPress to generate only the image sizes that are specified in the Media Settings (in the Admin Area), the above code could be modified like so:
// disable generated image sizes
function shapeSpace_disable_image_sizes($sizes) {
unset($sizes['medium_large']); // disable medium-large size
unset($sizes['1536x1536']); // disable 2x medium-large size
unset($sizes['2048x2048']); // disable 2x large size
return $sizes;
}
add_action('intermediate_image_sizes_advanced', 'shapeSpace_disable_image_sizes'); // disable scaled image size add_filter('big_image_size_threshold', '__return_false');
And so forth, you can dial in the perfect configuration and optimize your site’s image handling and server resources. Merry Christmas 🙂
Going further..
While the above technique is great for disabling any/all of the default generated image sizes, it’s still possible that your theme or some plugin is creating even more sizes. To learn more about this and how to disable any extra theme or plugin-related images, check out my in-depth tutorial at Perishable Press, How to Disable WordPress Automatically Generated Images – Complete Guide. There you’ll find everything you need to know about WordPress automatically generated images, Post Thumbnail images, important tips, and much more.
Unfortunately I am still seeing the auto generated images after adding this code. Is there something else I need to do to make it work? Thanks!
I’m not sure what might be happening in your case, but here are some things that may be useful for you:
The technique only work on images that are uploaded to the Media Library
The code snippets only work for newly uploaded images (it’s not retro-active)
You can verify the technique works properly on a default WP install w/ only default plugins and theme
If the technique is not working on your newly uploaded images, something (like a theme or plugin) is interfering with normal functionality. In which case, the best way to identify the issue is to do some basic troubleshooting. Here is a free troubleshooting guide for WordPress, specifically for this issue check out the sections on “Plugins” and “Themes” should be most useful.
I hope that helps, also here is a more in-depth tutorial on the topic that provides a lot more details, etc.
Is there a reason for the yellow on light grey text in your code examples? Like, you have Superman vision, unlike we mere mortals?
Sarcasm aside, great article & snippet; thanks!
Thanks for the feedback Chris! The site/design is not mine though, you can blame Elliott for any unpleasantness 🙂 I’ll ask if he can get it fixed up!
Apologies minor blip, should be fixed now 🙂
” return $sizes; “should be on a new line…
Thanks.. worked for me.
I have a WooCommerce site and I upload 20 photos to the gallery for each product. One day I accidentally noticed that for one of my products, the gallery size was 5 MB and I compared it to the wp-content folder and there it was 30 MB. And I’m depressed about my other 500 products.
I do not know how to thank you. It was wonderful post bro.
Hello
I am a photographer with a WP site I’ve had for over ten years on a server where my storage as unlimited. I have just moved to a new server, 50GB top limit. My uploads folder was a massively bloated 7.27GB. I have been going back through my images on my cPanel and deleting thumbnails. What I want is to have only the FULL size images (my default was 1600px width). I have managed to almost half my library size. But every time I regenerate my thumbs (setting everything to 0) it still creates the two default sizes. Your script looks really interesting. But I have three questions:
1: How can stop ALL sizes being generated
2: Where do I paste your code in functions.php to be safe and not break WP
3: I imagine I will have to add this modifications every time WP updates.
Thanks a lot in advance
Vishy
I think return $sizes; needs to go on a new line, it’s commented out right now
Please correct the typo in this line:
unset($sizes[‘2048×2048’]); // disable 2x large size return $sizes;
There should be a line break at the end of that comment. As it is, the $sizes array is never returned and no intermediate sizes are created at upload.
The return has now been fixed 🙂