2
votes

I have a theme that generates 11 different sized images for every image uploaded.

set_post_thumbnail_size( 220, 150, true);
add_image_size( 'redmag-blog-list', 491, 280, true);
add_image_size( 'redmag-blog-list-large', 614, 320, true);
add_image_size( 'redmag-sidebar', 100 ,100, true);
add_image_size( 'redmag-blog-grid', 496, 290, true);
add_image_size( 'redmag-blog-tran', 480 ,250, true);
add_image_size( 'redmag-blog-tran-vertical', 328 ,480, true);
add_image_size( 'redmag-blog-video',480,150,true);
add_image_size( 'redmag-mini-list', 150 ,100, true);
add_image_size( 'redmag-blog-tran-large', 770 ,420, true);
add_image_size( 'redmag-blog-vertical', 510 ,680, true);
add_image_size( 'redmag-related-image',370,247,true);

This is okay for the theme but it also resizes every image uploaded on Woocommerce which isn't required and takes up a lot of space.

As a temporary measure, I comment out the above code while adding images to woocommerce so it doesn't generate those images.

Can I disable the image generation for ONLY Woocommerce uploaded images?

2
You can try remove_image_sizeDmitry
@Dmitry I want it to be applied only for Woocommerce related images.Vinith Almeida

2 Answers

1
votes

You can try code below:

function remove_default_image_sizes( $sizes ) {

  /* Default WordPress */
  unset( $sizes[ 'thumbnail' ]);       // Remove Thumbnail (150 x 150 hard cropped)
  unset( $sizes[ 'medium' ]);          // Remove Medium resolution (300 x 300 max height 300px)
  unset( $sizes[ 'medium_large' ]);    // Remove Medium Large (added in WP 4.4) resolution (768 x 0 infinite height)
  unset( $sizes[ 'large' ]);           // Remove Large resolution (1024 x 1024 max height 1024px)

  /* With WooCommerce */
  unset( $sizes[ 'shop_thumbnail' ]);  // Remove Shop thumbnail (180 x 180 hard cropped)
  unset( $sizes[ 'shop_catalog' ]);    // Remove Shop catalog (300 x 300 hard cropped)
  unset( $sizes[ 'shop_single' ]);     // Shop single (600 x 600 hard cropped)

  return $sizes;
}

add_filter( 'intermediate_image_sizes_advanced', 'remove_default_image_sizes' );
0
votes

As far as I know (not sure if anything has changed) you can't, image sizes are created when you add them via New Media, but I'm leaving that to someone better informed.

You may try some workarounds like the one below tested and working but not checked for conflicts:

// theme image size
add_image_size( '1280', 1280, 9999 );

function mlnc_remove_add_image_sizes() {
    // remove theme image size
    remove_image_size( '1280' );
    // add product image size
    add_image_size('1165', 1165, true );
}
if ( get_post_type( $_REQUEST['post_id'] ) === 'product' ) { 
  add_action('init', 'mlnc_remove_add_image_sizes');
}

This is the basic idea...