For every image you upload, WordPress creates several resized copies. Besides thumbnail, medium and large, it also makes medium_large (768px wide), 1536x1536 and 2048x2048. If your design never uses those, they only fill the uploads folder and slow down every upload.
This snippet disables those WordPress default image sizes for new uploads and leaves thumbnail, medium, large and your theme’s own sizes alone.
The snippet
<?php
/**
* Stop WordPress from generating image sizes you never use.
*/
add_filter( 'intermediate_image_sizes_advanced', 'dpc_disable_image_sizes' );
function dpc_disable_image_sizes( $sizes ) {
// ===== Config: size names WordPress should not create =====
$disabled = array( 'medium_large', '1536x1536', '2048x2048' );
// ===== End of config =====
$disabled = (array) apply_filters( 'dpc_disabled_image_sizes', $disabled );
foreach ( $disabled as $name ) {
unset( $sizes[ $name ] );
}
return $sizes;
}
How it works
- When WordPress creates the sub-sizes of an upload, it passes the list of sizes through
intermediate_image_sizes_advanced. Each key is a size name, so removing a key means that file is never made. - The same filter is used when WordPress retries missing sizes after a failed upload, so the disabled sizes do not come back later.
- The sizes stay registered. Code that asks for
medium_largegets the next best file WordPress has, as it does for any missing size. - The old version used two filters and two functions for the same job, with an unprefixed function name. One list is easier to edit.
Add it with Scripts Organizer
In Scripts Organizer go to Code Blocks → Add New and give the block a name. Set Trigger location to Everywhere, tick PHP under Script location, paste the code into the PHP editor and publish. Leave Action/Hook empty. Uploads happen in wp-admin, the block editor (REST API) and front-end forms, and Everywhere covers all of them.
The same code also works unchanged in a small plugin or an mu-plugin.
Customise it
- Keep one of the sizes: remove its name from
$disabled. Many themes use1536x1536for retina images insrcset, so check before you remove it. - Disable a size added by a theme or plugin: add its name, for example
'woocommerce_single'. - Thumbnail, medium and large can also be switched off in Settings → Media by setting both their width and height to
0.
Gotchas
- Files that already exist are not deleted. Regenerate the thumbnails with a plugin or
wp media regenerateif you want to clean up, after you have checked that nothing links to them. - Images larger than 2560px are still scaled down to a
-scaledcopy. That is controlled by thebig_image_size_thresholdfilter, not by this list. - Fewer sizes also means fewer
srcsetcandidates. If large images look soft on some screens, keep1536x1536; see Images are not sharp enough when they are scaled down.
In the Scripts Organizer library Import it with one click from Scripts Organizer.