1

I'm trying to disable image sizes generation in wordpress.

When I use:

print_r( get_intermediate_image_sizes() );

it shows:

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

So i tried:

add_filter('intermediate_image_sizes_advanced', 'filter_image_sizes');
function filter_image_sizes($sizes) {
    unset( $sizes['medium']);
    unset( $sizes['medium_large']);
    unset( $sizes['large']);
    unset( $sizes['1536x1536']);
    unset( $sizes['2048x2048']);
    return $sizes;
});

and

add_filter('intermediate_image_sizes', 'filter_image_sizes');
function filter_image_sizes($sizes) {
    unset( $sizes['medium']);
    unset( $sizes['medium_large']);
    unset( $sizes['large']);
    unset( $sizes['1536x1536']);
    unset( $sizes['2048x2048']);
    return $sizes;
});

but it doesn't work. Setting values in admin menu doesn't work too.

add_filter( 'intermediate_image_sizes', '__return_empty_array', 99 );

this work's, but deletes all image sizes and I want to leave thumbnails..

1

Try this

function filter_image_sizes() {
    foreach ( get_intermediate_image_sizes() as $size ) {
        if (in_array( $size, array('medium', 'medium_large', 'large') ) ) {
            remove_image_size( $size );
        }
    }
}

add_action('init', 'filter_image_sizes');
4
  • Well, it deletes my woocommerce image sizes.
    – Matredok
    Nov 22 '19 at 19:16
  • Remove the "!" before in_array() Or retake my code, I have change it
    – Sam
    Nov 22 '19 at 19:21
  • It's better but it deletes '1536x1536', '2048x2048' sizes. Hmm...
    – Matredok
    Nov 22 '19 at 19:38
  • Remove the '1536x1536' and '2048x2048' size from the array ;) Or retake my code haha
    – Sam
    Nov 22 '19 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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