0

I've found several examples where they use the intermediate_image_sizes_advanced filter to unset the default WP image sizes that are generated whenever a user uploads a new image to the media library. This filter, on a brand new install with no plugins will not be called at all.

Calling get_intermediate_image_sizes() spits out the following array:

[05-Feb-2020 17:29:55 UTC] Array
(
    [0] => thumbnail
    [1] => medium
    [2] => medium_large
    [3] => large
    [4] => 1536x1536
    [5] => 2048x2048
)

Where did 1536x1536 come from? Where did 2048x2048 com from?

I later tried to call remove_image_size('size') from within an init action and this only worked for the two new sizes mentioned above. The other default sizes still remained:

[05-Feb-2020 17:30:26 UTC] Array
(
    [0] => thumbnail
    [1] => medium
    [2] => medium_large
    [3] => large
)

Does anyone know what's going on here? Has something changed?

3
  • Ive found another similar question regarding the new image sizes, apparently it's new since 5.3: wordpress.stackexchange.com/a/354379/57775.
    – eballeste
    Commented Feb 5, 2020 at 18:20
  • The accepted answer in the linked question also uses the intermediate_image_sizes_advanced filter. This filter is not working for me.
    – eballeste
    Commented Feb 5, 2020 at 18:21
  • note that some enterprise WP hosts remove all image sizes to prevent those files being created, and then use runtime URL parameters to get the sizes instead. WP VIP is known for this
    – Tom J Nowell
    Commented Feb 7 at 13:34

3 Answers 3

7

Using the intermediate_image_sizes_advanced filter was not working for me. However, the intermediate_image_sizes works and the $default_sizes array is an indexed array, not an associative array. In every single example that I have found for using the intermediate_image_sizes_advanced filter, the answer's are unsetting the image sizes from an associative array.

The following worked for me in WP 5.3:

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

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

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

  return $sizes;
}

Here's a link to the function that adds the two additional sizes since 5.3 with an explanation to why I was able to remove them using remove_image_size.

1
  • This solution worked for me where intermediate_image_sizes_advanced was not.
    – nabrown
    Commented Jun 7, 2021 at 20:33
0

you can used below code to remove image sizes is placed in the appropriate location within your theme or plugin.

after_setup_theme action hook used to achieve functionality.

function remove_default_image_sizes() {
    remove_image_size('thumbnail');
    remove_image_size('medium');
    remove_image_size('medium_large');
    remove_image_size('large');
}
add_action('after_setup_theme', 'remove_default_image_sizes');
0

In addition: the 1536x1536 and 2048x2048 sizes are not removed by the intermediate_image_sizes-filter. To remove them, I just use the remove_image_size-method.

add_action( 'init', 'wpdocs_remove_default_additional_image_sizes' );
function wpdocs_remove_default_additional_image_sizes() {
    remove_image_size( '1536x1536' );
    remove_image_size( '2048x2048' );
}

(from: https://developer.wordpress.org/reference/functions/_wp_add_additional_image_sizes/#comment-6722)

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Feb 13 at 22: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.