PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 5, 2022

[FIXED] Wordpress/WooCommerce remove default image sizes?

 February 05, 2022     php, woocommerce, wordpress     No comments   

Issue

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');

Solution

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>';
}


Answered By - dungey_140
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing