8
votes

I'm trying to dequeue/deregister styles that are unneccesarry(WooCommerce,Contact form 7) from all post/pages of my wordpress website.

Tried offical WooCommerce's way to disable these extra styles and scripts, but it's not dequeue-ing those since styles are still loaded in the source code and Google PageSpeed Insights is still showing them as being rendered; so:

add_filter( 'woocommerce_enqueue_styles', 'jk_dequeue_styles' );
function jk_dequeue_styles( $enqueue_styles ) {
    unset( $enqueue_styles['woocommerce-general'] );    // Remove the gloss
    unset( $enqueue_styles['woocommerce-layout'] );     // Remove the layout
    unset( $enqueue_styles['woocommerce-smallscreen'] );    // Remove the smallscreen optimisation
    return $enqueue_styles;
}

// Or just remove them all in one line
add_filter( 'woocommerce_enqueue_styles', '__return_false' );

in functions.php Isn't working. I also tried to remove them like this:

function remove_assets() {
    wp_dequeue_style('wc-block-vendors-style-css');
    wp_deregister_style('wc-block-vendors-style-css');
}
add_action( 'wp_print_styles', 'remove_assets', PHP_INT_MAX );

Pointing at specific style id wc-block-vendors-style-css and setting PHP_INT_MAX. It's still not removing that specific style.

enter image description here

Notes:

This functions.php belongs to the child theme of a custom, redone twenty-twenty WordPress theme and cache has been cleared. I've tried most of the answers here from the StackOverflow of users' having similar issues, but not a single one worked.

Woocommerce support is enabled in the theme.

How to dequeue/deregister styles and scripts with WordPress 5.6 in 2021?

3
In addition to the answer provided... Your Official answer uses the incorrect function. gist.github.com/woogists/… - Howard E
@HowardE That snippet is part of the woocomerce docs source I put in question, tried that one as well, not working. All WooCommerce styles and scripts are still loaded - Ognj3n
The gist says add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' ); what you put above is __return_false - Howard E
docs.woocommerce.com/document/disable-the-default-stylesheet/… read this; your suggestion is in there, tried it, it's not working.. - Ognj3n
In this case, you should ensure that your functions are actually running. You can try dumping to error_log something to validate your functions are triggering. - Howard E

3 Answers

1
votes

According to @Aliakseyenka Ihar - I've tested myself, When I added deregister before wp_dequeue_style - it removes the vendor-style

function remove_assets() {
    wp_deregister_style('wc-block-vendors-style');
    wp_dequeue_style('wc-block-vendors-style');
}
add_action( 'wp_enqueue_scripts', 'remove_assets', 11);

Or ninja way

add_filter( 'wp_enqueue_scripts', 'jk_dequeue_styles', 99999 );
function jk_dequeue_styles( $enqueue_styles ) {
    global $wp_styles;
    unset ( $wp_styles->registered['wc-block-vendors-style'] );
}
0
votes

If your stylesheet is registered and enqueued correctly then...

function dequeue_css() {
    wp_dequeue_style('wc-block-vendors-style');
    wp_deregister_style('wc-block-vendors-style');
}
add_action('wp_enqueue_scripts','dequeue_css');
// add a priority if you need it
// add_action('wp_enqueue_scripts','dequeue_css',100);

Or you can try this way:

add_filter('style_loader_src', function($href){
    if(strpos($href, "wc-block-vendors-style") !== false) {
        return false;
    }
    return $href;
});
0
votes

I've figured out that in order to remove those styles the Woocomerce integration in the active theme was required.

add_theme_support( 'woocommerce' );

This solved my issue.