Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Remove WordPress image sizes from WooCommerce product types and the other way around WordPress

  • REFUNDED

Hello,
I'm running WordPress along with WooCommerce and have started to run out of space on the server, mostly because of the big amount of image sizes that WP and WooCommerce creates.

Thus, I'm using this function that is supposed to only let WooCommerce create it's image sizes when uploading the image to a product content type. It also makes sure that the default WordPress image sizes are not created on product types:

function remove_unused_image_sizes() {
if ( get_post_type($_REQUEST['post_id']) != "product") {
remove_image_size( 'woocommerce_thumbnail' );
remove_image_size( 'woocommerce_single' );
remove_image_size( 'woocommerce_gallery_thumbnail' );
remove_image_size( 'shop_catalog' );
remove_image_size( 'shop_single' );
remove_image_size( 'shop_thumbnail' );
}
if ( get_post_type($_REQUEST['post_id']) == "product") {
remove_image_size( 'thumbnail' );
remove_image_size( 'medium' );
remove_image_size( 'medium_large' );
remove_image_size( 'large' );
}
}
add_action('init', 'remove_unused_image_sizes');


But, this function only works for new content that I create, I also need to find a way to remove the WordPress image sizes on the big amount of images that has been uploaded to products (thumbnail, medium, medium_large and large) and vice versa, remove the WooCommerce image sizes on attachments that has been uploaded everywhere else so to say.

So, this is what I want help with:

1. Evaluate the function I have posted above and make sure it will work for the scenario I describe for uploads made in the future.

2. Come up with a way for me to be able to delete the unused image sizes (wordpress default) on existing product post types and only keep the WooCommerce ones. And of course the other way around, let me remove the unused image sizes from WooCommerce on all other existing places of the site that are not uploaded to a product post type.

It won't be a problem that the WooCommerce images won't be available in the WordPress dimensions on other parts of the site. And the same goes for the "normal" WordPress images. Those are not needed on WooCommerce pages either.

Let me know if you have questions!

Answers (3)

2021-10-27

Arnav Joy answers:

why don't you use any plugin such as this: https://wordpress.org/plugins/wp-smushit/


Jens Filipsson comments:

Of course I'm using an optimizer, everything is optimized with webP images and what not, but that plugin won't solve the question I'm asking.

2021-10-27

Hariprasad Vijayan answers:

Hello Jens,
Once the image size is removed, regenerate thumbnail then the unused images will be removed from server. You can use plugin https://wordpress.org/plugins/regenerate-thumbnails/
Also optimise images using image optimise plugin https://wordpress.org/plugins/wp-smushit/


Jens Filipsson comments:

Hey Hari,
long time no see!

No, it won't work in this case I believe since the images size definitions will be in place and active, but will only be disabled when you upload something. Thus I need to know if the image is uploaded to a product and if that's the case, remove the WordPress sizes.

And, if it's not uploaded to a product, remove the WooCommerce generated sizes but keep the WordPress ones.


Hariprasad Vijayan comments:

Hey Jens, hope you are good.

https://stackoverflow.com/questions/61488834/disable-multiple-size-image-generation-for-woocommerce-uploaded-images-only This is one thread that similar to your requirement.

function remove_unused_image_sizes() {
if ( get_post_type( $_REQUEST['post_id'] ) === 'product' ) {
remove_image_size( 'thumbnail' );
remove_image_size( 'medium' );
remove_image_size( 'medium_large' );
remove_image_size( 'large' );
} else {
remove_image_size( 'woocommerce_thumbnail' );
remove_image_size( 'woocommerce_single' );
remove_image_size( 'woocommerce_gallery_thumbnail' );
remove_image_size( 'shop_catalog' );
remove_image_size( 'shop_single' );
remove_image_size( 'shop_thumbnail' );
}
}
add_action('init', 'remove_unused_image_sizes');
This would probably work when you upload new image.(Not tested).
It would be hard to remove already uploaded images because regeneration of image happens globally, so we won't get if its attached to a product or not. https://betterstudio.com/blog/regenerate-thumbnails-woocommerce/
Will share if i have some other thoughts.


Jens Filipsson comments:

Yeah, I think a custom function might be needed that loops through images and check if they are attached to a product and if so, remove the unnecessary image sizes of that particular image. Not sure if it's possible to see what post type it's attached to and to be able to remove some image sizes and keep some?


Jens Filipsson comments:

Do you have any other ideas, otherwise I think I will go ahead and ask for a refund now..

2021-10-28

timDesain Nanang answers:

for this purpose, you can set woocommerce image size as same as default wordpress media size.
you can try these step
1. put the following code in the functions.php
2. go to menu: Settings - Media to running part #2
3. instal plugin https://wordpress.org/support/plugin/force-regenerate-thumbnails/ to clean up the images that has been uploaded


//#1. disable woo image regeneration
add_filter( 'woocommerce_background_image_regeneration', '__return_false' );


//#2. remove medium_large size from wp_options
//go to menu: Settings - Media to delete it
add_action( 'admin_init', 'timd_image_size_update' );
function timd_image_size_update() {
global $wpdb, $pagenow;

if( !current_user_can( 'upload_files' ) ) return;
if( $pagenow <> 'options-media.php' ) {
delete_option('medium_large_size_w');
delete_option('medium_large_size_h');
}
}

//#3. remove unused image sizes
remove_image_size('medium_large');
remove_image_size('1536x1536');
remove_image_size('2048x2048');


//#4. override woocommerce media size with WordPress
//single
add_filter( 'woocommerce_get_image_size_single', 'timd_image_single');
function timd_image_single($size) {
//set the size as same as with WordPress large size
return array( 'width' => 444, 'height' => 444, 'crop' => 1, );
}
//grid
add_filter( 'woocommerce_get_image_size_thumbnail', 'timd_image_grid');
function timd_image_grid($size) {
//set the size as same as with WordPress medium size
return array( 'width' => 444, 'height' => 444, 'crop' => 1, );
}
//nav
add_filter( 'woocommerce_get_image_size_gallery_thumbnail', 'timd_image_gallery_nav');
function timd_image_gallery_nav($size) {
//set the size as same as with WordPress thumbnail size
return array( 'width' => 444, 'height' => 444, 'crop' => 1, );
}
//zoom-in
add_filter( 'woocommerce_gallery_full_size', 'timd_image_zoom');
function timd_image_zoom($size) {
return 'large';
}


Jens Filipsson comments:

Good idea, except that I still need the WooCommerce image sizes to be respected, since they should be in different sizes than the WordPress ones.


Jens Filipsson comments:

Do you have any other ideas, otherwise I think I will go ahead and ask for a refund now..