Skip to main content
Snippets

Enqueue and Dequeue Styles and Scripts in WordPress

By April 29, 2016No Comments

I am often faced with a premium theme bogged down with too many resources being fetched on page load. The following helps to get rid of bloat.

Stylesheets

If you look in the source of the page you will notice the ID for any given stylesheet as so:

<link rel='stylesheet' id='style-id'  href='http://yoursite.com/style.css' type='text/css' media='all' />

Use the ID to target that resource and modify the following code:

//
// Remove Unwanted Stylesheet
//
function remove_unwanted_stylesheets() {

    wp_dequeue_style( 'style-id' );
    wp_deregister_style( 'style-id' );

}
add_action( 'wp_enqueue_scripts', 'remove_unwanted_stylesheets', 999 );
//
// You can also do this if the theme / plugin author used the print styles action
//
remove_action('wp_print_styles', 'some_style');

Scripts

The same is for scripts, however there are some tricky bits depending on how the plugin / theme developer structured their work.

//
// Remove unwanted scripts
//
add_action( 'wp_print_scripts', 'dequeue_scripts', 9999 );

function dequeue_scripts() {

	wp_dequeue_script( 'script-name' );
}

There are also actions that can load unwanted resources. The add_action(); function can be removed so the given resource doesn’t load. In this example the action to be removed was written to fire at the wp_print_styles, so we target that accordingly.

Contact us to learn more about Adopt the Web for your business

Author Jarod Thornton

More posts by Jarod Thornton