0

I'm trying to remove the default image sizes that are created given a specific image size threshold.

Listed using this:

get_intermediate_image_sizes() // thumbnail [1] => medium [2] => medium_large [3] => large [4] => 1536x1536 [5] => 2048x2048

I've found this method remove_image_size which doesn't work for the default ones.

I've found this snippet:

add_filter( 'intermediate_image_sizes_advanced', function($sizes){
    unset( $sizes['small']); 
    unset( $sizes['medium']);
    unset( $sizes['large']); 
    unset( $sizes['medium_large']); 
    return $sizes;
});

Which appears to do nothing.

Anyone got an idea as to how I'd go about removing the default image sizes?

Edit: The accepted answer is correctly how you remove the default image_sizes globally. If you'd rather want to switch between when different image_sizes should be active for which specific image assets then there's a way to do that here: image_size with respect to aspect ratios

2

1 Answer 1

2

To unset all default image sizes in WordPress using PHP you can use this function:

add_filter( 'intermediate_image_sizes', 'remove_default_img_sizes', 10, 1);

function remove_default_img_sizes( $sizes ) {
  $targets = ['thumbnail', 'medium', 'medium_large', 'large', '1536x1536', '2048x2048'];

  foreach($sizes as $size_index=>$size) {
    if(in_array($size, $targets)) {
      unset($sizes[$size_index]);
    }
  }

  return $sizes;
}

Alternatively, you can also unset default images by going into the WordPress dashboard. Then proceeding to Settings -> Media and put all value's on 0. See image for reference:

enter image description here

9
  • 1
    The first one is the snippet I included in the question. It doesn't do anything. I've seen people do that setting, but I'd prefer doing it with code
    – Fluxian
    Sep 21, 2022 at 12:26
  • 1
    Hmm that's weird, do you have any other PHP functions that interfear with the function? Try to insert an echo in the function and see if you see any visual feedback, there might be an error in your functions.php Sep 21, 2022 at 12:27
  • There are no errors reported in functions.php. Echo'ing inside the function doesn't show up anywhere. Echo'ing after the hook works just fine.
    – Fluxian
    Sep 21, 2022 at 12:31
  • 1
    Plain Wordpress
    – Fluxian
    Sep 21, 2022 at 12:33
  • 1
    Okay, then the last thing i can think of is that the default image sizes are not really the default? can you check what the width and height are of your images? Because if they are custom you can use unset( $sizes['1280x720']); for instance, to unset custom sizes. Sep 21, 2022 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.