WordPress Dequeue, to Load in Less and Merge Files

Written By :

Category :

Updates

Posted On :

Share This :

In general, most sites are designed with a WP theme in mind to load in just about everything under the umbrella for the purpose of you might use that. In the post 2020 web world that changed significantly as mobile devices simply cannot process threaded connections like a desktop which caused issues loading multiple files had to be done one at a time.

Sorry about the history lesson.

However with Dev.Land 3.0.1 you can merge in your files your theme uses, but then you will need to dequeue scripts or styles and figure out what files you merged should be also loading not through the enqueue method, but through the combined merged files system.

Here is some code to explain how to do this, in the code below we echo out the handler of the registered files. Then if we combined those files we tell WordPress to simply dequeue those files.

function webdevland_removal() {
		// Find the handler that is registered
		global $wp_scripts, $wp_styles;
		echo "Registered Script Handles:<br>";
		echo "<pre>";
		print_r($wp_scripts->registered);
		echo "</pre>";
		echo "Registered Style Handles:<br>";
		echo "<pre>";
		print_r($wp_styles->registered);
		echo "</pre>";
		// Remove Global Styles and Scripts
		wp_dequeue_script('jquery');
		wp_dequeue_script('migrate');
		wp_dequeue_style('wp-block-library');
		wp_dequeue_style('gutenverse-google-font');
		wp_dequeue_style('gutenverse-frontend-font-awesome' );
		wp_dequeue_style('zeever-style');
		wp_dequeue_style('zeever-core-add');
		wp_dequeue_style('zeever-animate');
		wp_dequeue_style('gutenverse-frontend-icon-gutenverse');
		wp_dequeue_style('gutenverse-frontend-style');
		wp_dequeue_style('gutenverse-frontend-icons');
		// Remove from pages that are not the homepage
	if (!is_front_page()) {
		wp_dequeue_script('react-player-dep');
		wp_dequeue_script('gutenverse-frontend-event');
	}
}
add_action( 'wp_enqueue_scripts', 'webdevland_removal', 100 );

This is an elegant solution to load what you need where you need it you can even setup certain pages to load certain assets for example I’m not loading the video player on my entire website so only the homepage will load the video player this will give better performance to my entire site based on what is needed. I can also look into merging the video player code, but found that to be problematic combining with other scripts, so this solutions worked best.

For further comments or suggestions consider reaching out to us.