Home Blogging Blogging Tips How to speed up your WordPress blog by excluding certain JavaScript and...

How to speed up your WordPress blog by excluding certain JavaScript and CSS files

114
SHARE

cravingtech

If you own a WordPress blog, you should know by now that the more plugins you’ve got, the slower your blog/website gets. This is because each installed and activated plugin usually has their own JS (JavaScript) and CSS (Cascading Style Sheets) running on every page and post.

Obviously, you don’t need them to be running on every part of your site.

For example, you don’t need a Contact form plugin to load their resources when a visitor is browsing your articles. Or in my case, I don’t need my reviews-related plugins to load their JS and CSS files if my reader is not browsing a review article.

By tweaking the functions.php that comes with your installed theme (or with the Child theme), you can add codes to check and deregister these unnecessary scripts and styles. Ideally, you should be using a child theme so any modifications you make to the functions.php is not lost when the theme gets an update.

Here is a snippet from my functions.php:

deregister

In order to be able to deregister the JS and CSS files, you need to know the name of the handles on each of the plugin which files you want to deregister. I use a plugin called “WP Asset CleanUp” to find out about the handles on a post type and then add them to my_deregister function.

JS CSS handle

WP Asset Cleanup does a similar thing to what we are doing, but it’s quite limited in my opinion. It cannot do post categories or tags and therefore, we’d need to do these manually using the functions.php.

On my example above, I unload many scripts and files that are only necessary if my readers are on my Gadget Reviews posts. You can further tweak the script to suit your needs. For example, you probably want to deregister commenting-related scripts and styles at your home page, and so on (using is_page, is_home, etc).

After the tweaks, I’ve managed to gain a 15% speed improvement which is not bad at all.

There are also others that you might want to disable such as Emoji support on WordPress (if you don’t use Emojis on your blog) or comment-related files if you disable comments on your blog.