Remove of defer plugin related stylesheets

Viewed 262

I have a site running with WooCommerce and WPBakery.

I'm trying to reduce unused CSS requests as highlighted by Page Speed Insights.

Currently, here are the CSS files that are being generated by the two plugins:

<link rel="stylesheet" id="wc-blocks-vendors-style-css" href="http://theme.test/wp-content/plugins/woocommerce/packages/woocommerce-blocks/build/wc-blocks-vendors-style.css?ver=1629402699" type="text/css" media="all">

<link rel="stylesheet" id="wc-blocks-style-css" href="http://theme.test/wp-content/plugins/woocommerce/packages/woocommerce-blocks/build/wc-blocks-style.css?ver=1629402699" type="text/css" media="all">

<link rel="stylesheet" id="js_composer_front-css" href="http://theme.test/wp-content/plugins/js_composer/assets/css/js_composer.min.css?ver=6.7.0" type="text/css" media="all">

I'm looking to either:

  1. De-register those stylesheets (as I have tried below).
  2. defer all three of these stylesheets

I have tried the following below:

function remove_plugin_styles() {
  wp_deregister_style( 'wc-blocks-style' );  // woocommerce
  wp_deregister_style( 'wc-blocks-vendors-style-css' ); // woocommerce
  wp_deregister_style( 'js_composer_front-css' ); // js composer (wpbakery)
}
add_action( 'wp_enqueue_scripts', 'remove_plugin_styles', 100 );

With the above wc-blocks-style and wc-blocks-vendors-style-css are successfully removed. But, js_composer_front-css is still loaded in.

Is there a way to either defer all these sheet (and any other plugin related css files), or remove them?

1 Answers

defer (and async, for that matter) are specifically for <script> tags and don't apply to <link> stylesheets, and thus you can't really defer them in the traditional sense.

That said, your issue is that you're trying to deregister the wrong handle. The -css is automatically appended to enqueued CSS files by WordPress itself.

Changing the last deregister to:

wp_deregister_style( 'js_composer_front' ); // js composer (wpbakery)

Will properly deregister it

Related