John Davidson

php - Remove unused styles from wordpress

0 comments
Message:


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.



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?



My Answer:
To dequeue/deregister styles and scripts in WordPress 5.6 in 2021, you can try the following methods:

1. Use the wp_dequeue_style() and wp_deregister_style() functions in your functions.php file:

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


Make sure to use the correct handle for the style you want to remove.

2. If the above method doesn't work, you can try using the wp_dequeue_style() function directly in your theme's header.php file:

php
wp_dequeue_style('wc-block-vendors-style-css');


3. If you want to remove all WooCommerce styles, you can use the following code in your functions.php file:

php
add_filter( 'woocommerce_enqueue_styles', '__return_false' );


4. Make sure to clear your browser cache and any caching plugins you may have installed after making these changes to see the effects.

If none of these methods work, you may need to investigate further to see if there are any other dependencies or conflicts causing the styles to still be loaded.

Rate this post

4 of 5 based on 7064 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us