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
  • 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
  • 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
    – FUZIION
    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
  • Okay then there must be a plugin or something else interfering with the function. What plugins do you use?
    – FUZIION
    Sep 21, 2022 at 12:31
  • Only ACF nothing else.
    – Fluxian
    Sep 21, 2022 at 12:32

Your Answer

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

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