Too many resources – CSS and js.
When it comes to software development for the web, we like many other developers, use the WordPress amongst others CMS platforms. Many choose the platform because it’s simple to deploy, however, this comes with several drawbacks. Using WordPress does present many challenges for web developers, principal amongst these are an over reliance on plugins.
Okay, we’ve all heard that “less is more” when it comes to crafting a WordPress website, and this remains to be true.
Plugins and more plugins…
The solution to these issues is to dequeue scripts and styles when not required, unfortunately, many don’t do this because they do not or can’t be bothered to do a little research.
To give you an idea of what the fuss is all about, we take a look at one of the most widely used and popular plugins Contact Form 7. We shalln’t bore you with the details but here’s an example of how to go about the task of dequeuing the plugin when it’s not required. The process used is the same for all plugins files.
Finding the $handle
(string) The slug of the script to dequeue.
This is perhaps the most difficult single thing to establish, although you can often find this within the page source using view page source from your browser. In the case of Contact Form 7 it handle is contact-form-7.
We’ll assume that you’ve created a child theme. You’ll now need to create a blank functions.php file within your child theme. This can be simply acheived using FTP software, but make sure that it’s place within the child theme, otherwise, once a theme is updated you’ll lose your changes!
Your functions.php file should look like this:
and we now need to add a custom function specifically aimed at dequeuing scripts. Here we go...
That's it the contact form 7 script has been successfully dequeued throughout the entire website, however, we still need the script for any contact page, or any page that requires a contact form. So we now need to add extra lines to complete the contact form 7 function. We show complete code below.
So the script will only ever appear on the 'Contact' page or page '12' depending on how you've setup your Permalinks within WordPress.
Contact Form 7 additional information.