How to prevent Genesis Responsive Slider’s CSS and JS files from loading sitewide

Posted on
5/5 - (368 votes)

In StudioPress forum a user asks,

I’m using Genesis responsive slider.

I only need the slider to load on the home page.

Does anyone know of a copy/paste code snippet to be able to stop the plugin’s resources (js & CSS) from loading on every page of a site it’s installed on? Such as by naming IDs or page slugs for it to be loaded on?

Unlike the excellent Soliloquy, Genesis Responsive Slider, unfortunately, loads its assets on all pages of the website and not just on pages where the slider appears on the front end.

Here’s how we can dequeue Genesis Responsive Slider’s CSS and JS files on all pages except the front page on your Genesis site.

/**
 * Dequeue the Genesis Responsive Slider stylesheet.
 *
 * Hooked to the wp_print_styles action, with a late priority (100),
 * so that it is after the style was enqueued.
 */
add_action( 'wp_print_styles', function() {
	// if we are on front page, abort.
	if ( is_front_page() ) {
		return;
	}

	wp_dequeue_style( 'slider_styles' );
}, 100 );

/**
 * Dequeue the Genesis Responsive Slider script.
 *
 * Hooked to the wp_print_scripts action, with a late priority (100),
 * so that it is after the script was enqueued.
 */
add_action( 'wp_print_scripts', function() {
	// if we are on front page, abort.
	if ( is_front_page() ) {
		return;
	}

	wp_dequeue_script( 'flexslider' );
}, 100 );

Code goes in child theme’s functions.php.

Here’s a plugin that possibly does the same: https://wordpress.org/plugins/wp-asset-clean-up/ (untested)

Reference: wp-content/plugins/genesis-responsive-slider/genesis-responsive-slider.php