WordPress: Remove Unwanted Scripts and Stylesheets

| Ngoc Nguyen

I use NextGen gallery to organize my images and photos, but I don’t use it to display the galleries on my site. In order to optimize site speed, I wanted to prevent Nextgen scripts and stylesheets from loading.

Getting the Scripts and Stylesheets Handle

Before, we can remove the scripts and stylesheets from loading, we need to know their handle names. The easiest way to do this is to edit the functions.php file. Afterwards we simply navigate to any page and the handle names will be printed out at the top.

function wpcustom_inspect_scripts_and_styles() {
	global $wp_scripts;
	global $wp_styles;

	// Runs through the queue scripts
	foreach( $wp_scripts->queue as $handle ) :
		$scripts_list .= $handle . ' | ';

	// Runs through the queue styles
	foreach( $wp_styles->queue as $handle ) :
		$styles_list .= $handle . ' | ';

	printf('Scripts: %1$s 
Styles: %2$s', $scripts_list, $styles_list); } add_action( 'wp_print_scripts', 'wpcustom_inspect_scripts_and_styles' );

Removing the Unwanted Scripts and Stylesheets

Now that we have the handle names, we can call wp_deregister_style() to remove the unwanted scripts and stylesheets. In the sample below, I am removing the Nextgen scripts and lightbox stylesheets from firing throughout the site. If you need to remove them only for specific pages or sections, you can use WordPress’ conditional tags.

function wpcustom_deregister_scripts_and_styles(){
	wp_deregister_script('comment-reply ');

add_action( 'wp_print_styles', 'wpcustom_deregister_scripts_and_styles', 100 );

You May Also Like: