Remove WordPress Default Image Sizes
WordPress image sizes contain defaults, small, medium and large as well as medium-large which is used in their implementation of responsive images. You can remove these default images with a WordPress filter intermediate_image_sizes_advanced
Why you may want to do this could be that you definitely don’t need certain sizes or you are saving on web disk space or have a similar custom image size already created.
add_filter( 'intermediate_image_sizes_advanced', 'prefix_remove_default_images' ); // Remove default image sizes here. function prefix_remove_default_images( $sizes ) { unset( $sizes['small']); // 150px unset( $sizes['medium']); // 300px unset( $sizes['large']); // 1024px unset( $sizes['medium_large']); // 768px return $sizes; }
So just unset the ones you want, so for existing images nothing will change unless you use Force Regenerate Thumbnail which will remove redundant images – but for new images uploaded they will only take on the sizes which remain set by the filter.
You can also remove the srcset responsive images technique that WordPress enagages by using the max_srcset_image_width filter
// Disable WordPRess responsive srcset images add_filter('max_srcset_image_width', create_function('', 'return 1;'));
In what files (and which folders) can I apply these codes? wp-config.php?
Sorry I’m newb
In your themes’ functions.php
@victor
Just to let you know, there is no need to check to see if the size exists. Unset doesn’t throw any warnings or errors and is safe to use on variables that don’t exist. Checking if they exist only ends up adding a small amount of time to the function without providing any benefit.
The example in the main article is the best way to do this.
wrong…
function remove_default_image_sizes($sizes)
{
$needles = [‘thumbnail’,’medium’,’medium_large’,’large’];
foreach ($needles as $needle) {
if (($key = array_search($needle, $sizes)) !== false) {
unset($sizes[$key]);
}
}
return $sizes;
}