3
votes

Is it possible to remove default images from Wordpress? More specifically, those create by WooCommerce? I'm building a completely custom theme, for which I have no need for any of the WC image sizes, so I'd rather not have to store these on our server. I have experimented with the codex remove_image_size() in the below code, however this breaks the media library.

Thanks.

// Remove default WC image sizes
function wpdocs_remove_plugin_image_sizes() {
    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', 'wpdocs_remove_plugin_image_sizes');
2
Perhaps you should keep one of the default thumbnail wich could be required by the media library.gael

2 Answers

3
votes

After some further debugging, it turns out the above code does in fact work, but was being broken by another piece of code in the same file.

I couldn't find much referencing this request online so leaving the answer here for anyone else looking for the same.

Place in functions.php

// Remove default WC image sizes
function remove_wc_image_sizes() {
    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_wc_image_sizes');

This should also work for any other registered image sizes in Wordpress, all you need to do is find out the name of the image size you want to remove, and add it to the list above. The list above currently removes ALL WooCommerce image sizes so that you are left with only the Wordpress default sizes, and any other custom sizes you may have defined.

If you are unsure as to what sizes are registered in your theme, use the below code to display an array() list in the admin, to help you easily identify.

Place in functions.php

add_action( 'admin_init', 'theme_additional_images' );
// Display all image sizes other than the custom, default, thumbnail, medium and large

function theme_additional_images() {
    global $_wp_additional_image_sizes;
    $get_intermediate_image_sizes = get_intermediate_image_sizes();

    echo '<pre>' . print_r($_wp_additional_image_sizes) . '</pre>';
}
0
votes

I just want to say thanks for the code and add that I have modified it so that the WooCommerce image sizes are removed in all cases except when adding media to a product itself. In this case images uploaded to a product post on the post edit screen will still have the WooCommerce sizes while all other uploads elsewhere on the site will not. I needed this functionality as I use different image sizes for my small WooCommerce store to the rest of my main site. I didn't want the WooCommerce sizes cluttering up the server for every other image on the site. I'm just sharing this answer in case it helps anyone else :) `

function remove_wc_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' );
  }
}
add_action('init', 'remove_wc_image_sizes');