Easy way to use Dequeue Styles and Scripts In WordPress to Improve page loading time

What is enqueue in WordPress?

Enqueueing is the process of loading Javascript files or JS files -— including scripts and styles — to WordPress in a way that lets you use them whenever you need them without requiring the rewriting of code.
By enqueueing scripts, you tell WordPress which assets you want to add. WordPress will then automatically link those assets in the header and footer.

If you’re using a WordPress theme and sometimes you might don’t want to see some certain CSS (styles) or JS (scripts) files from your WordPress theme. And in case you’re in good standing to use the strong parent-child theme relationship in your project and willing to remove unnecessary styles and scripts.

Many WordPress plugins load scripts and styles on every website page, even though they may just be needed on a few specific pages. For example, WooCommerce loads at least 3 scripts and 4 styles on every page, even if e-commerce functionality (like a cart or products) isn’t shown on those pages.

You can dequeue & deregister your styles & scripts files by passing them into this two action hooks:
wp_print_scripts
wp_print-styles
and simple hook it differently.

To dequeue a stylesheet, use wp_dequeue_style plus the handler name in the quote
To dequeue a Javascript, use wp_dequeue_script plus the handler name in the quote
, and you’re done; now let’s see the example of code, and in the example, it’ll remove lightbox.css and jquery.mfp-lightbox.js files.

dequeue Styles:

//Dequeue Styles
function smartmag_dequeue_unnecessary_styles() {
    wp_dequeue_style( 'smartmag-magnific-popup' );
    wp_deregister_style( 'smartmag-magnific-popup' );
}
add_action( 'wp_print_styles', 'smartmag_dequeue_unnecessary_styles' );

dequeue Scripts:

//Dequeue JavaScripts
function smartmag_dequeue_unnecessary_scripts() {
    wp_dequeue_script( 'magnific-popup' );
    wp_deregister_script( 'magnific-popup' );
}
add_action( 'wp_print_scripts', 'smartmag_dequeue_unnecessary_scripts' );
0 Points