How to Defer Parsing of JavaScript in WordPress

How to Defer Parsing of JavaScript in WordPress

If you want to cut your website’s loading time, you can try to defer parsing of JavaScript. It allows the browser to render JavaScript only after it finishes loading the main content of a site. In this tutorial, we will cover several ways on how to defer parsing of JavaScript in WordPress.

Upgrade to WordPress Hosting with Hostinger today! With a guaranteed 99.99% uptime!

Get Your Discount Here

Why Should You Defer Parsing of JavaScript?

When you open a website, your browser will receive the site’s content from the server and loads the code from top to bottom. However, if it finds JavaScript, the loading process will be interrupted until it finishes downloading all the JavaScript.

To solve this issue, you can defer parsing of JavaScript, which allows the browser to load the full content without waiting for the scripts to load. By implementing this task, JavaScript parsing won’t negatively affect your website’s loading time.

Keep in mind that having lots of JavaScript isn’t bad. After all, It’s one of the essential web components, which allows you to extend the functionality of your site.

The point is, there are two main benefits you can get:

  • Better user experience – visitors won’t have to wait long to view your website content.
  • Improved SEO rankingGoogle bots can crawl your site faster.

How to Defer Parsing of JavaScript in WordPress?

This section will show you a step-by-step guide to defer parsing of JavaScript. To top it off, we’ll also show you how to analyze the problem and test the changes after applying the task.

1. Analyze the site

To find out whether you should defer parsing of JavaScript in your WordPress website, analyze it using site speed testing tools like GTMetrix. As an example, here is the performance result of a website before implementing the method:

analyze defer parsing of javascript in gtmetrix

It shows that there’s an issue regarding JavaScript parsing, which means the website site needs to defer it.

2. Defer Parsing of JavaScript in WordPress

Now, let’s get into the steps of deferring parsing of JavaScript! There are two ways to do it – installing a plugin or adding a code snippet manually.

Using WordPress plugins

banner of speed booster pack

One of the easiest ways to defer JavaScript parsing is by using a plugin. Speed Booster Pack is one of the most popular plugins for this task. To begin, install and activate it, then follow these steps:

  1. Go to your WordPress Dashboard
  2. On the left sidebar, navigate to Speed Booster
  3. Click the Advanced tab, and activate Defer parsing of JS files
    defer parsing of wordpress javascript using speed booster pack plugin
  4. Once done, tap Save Changes

Defer Parsing of JavaScript via functions.php

Alternatively, you can carry out the task manually by editing one of the WordPress core files. Make sure to backup your website before proceeding. Here are the steps:

  1. From your WordPress admin area, go to Appearance -> Theme Editor
  2. Select functions.php in the Theme Files
    open theme editor to defer parsing of javascript
  3. Enter this code at the bottom of the file:
    function defer_parsing_of_js ( $url ) {
    if ( FALSE === strpos( $url, '.js' ) ) return $url;
    if ( strpos( $url, 'jquery.js' ) ) return $url;
    return "$url' defer ";
    add_filter( 'clean_url', 'defer_parsing_of_js', 11, 1 );
  4. Click Update File.

3. Test the Changes

After using one of the above options, re-analyze your site with GTMetrix:successful defer parsing of javascript in gtmetrix


In this tutorial, you’ve learned how to improve your site performance by deferring parsing of JavaScript. You can do so by using a plugin or adding a code snippet to the theme’s functions.php file.

Keep in mind that improving your website’s loading time will lead to better user experience and improved SEO ranking.

Do you have any questions regarding this topic? Share your thoughts in the comment section below!

The Author


Domantas G. / @domantas

Domantas leads the content and SEO teams forward with fresh ideas and out of the box approaches. Armed with extensive SEO and marketing knowledge, he aims to spread the word of Hostinger to every corner of the world. During his free time, Domantas likes to hone his web development skills and travel to exotic places.


Brian Reply

April 13 2017

Hi, thanks for the article. I tried the code both in my Wordpress wp-includes/functions.php file and my theme functions.php file and neither worked. Do you have any idea why it wouldn’t work?



    Replied on April 13 2017

    Thank you for contacting us. In some cases, the specific configuration in WordPress (.htaccess, specific plugins or other settings) may cause issues when trying to defer parsing of JS. In such cases, we would recommend deferring parsing of JavaScript via WordPress plugins. For more specific information, see Option 1 found in Step 2


Candell Reply

April 13 2017

Thanks for this, it works great with one exception, it caused Yoast SEO to break. Can we exclude Yoast from being deferred?



    Replied on April 13 2017

    It is possible to exclude scripts from being deferred when using the “Speed Booster Pack” plugin. You will be required to access the Settings section of your plug-in, and there will be a selection that says “Exclude scripts from being deferred.” There, you will need to specify the JS file that you wish to exclude. For example `jqueryfile.min.js. If you are planning on excluding more than 4 scripts, the plugin authors suggest to disable deferring parsing of JavaScript altogether, as your page speed score might be affected.



    Replied on May 12 2017

    you need yoast SEO plugin to work only on wp-admin section, so bypass deferring using this: if ( is_user_logged_in() ) {} else { /* content written above */ }



    Replied on May 18 2017

    use this to not break Yoast SEO (it checks if user is logged in) if ( is_user_logged_in() ) {} else { function defer_parsing_of_js ( $url ) { if ( FALSE === strpos( $url, '.js' ) ) return $url; if ( strpos( $url, 'jquery.js' ) ) return $url; return "$url' defer "; } add_filter( 'clean_url', 'defer_parsing_of_js', 11, 1 ); }


123project Reply

April 13 2017

Hi I tried both codes, both of them worked but I think this method is not compatible with woocomerce because many woocomerce features not work when inserting that code!


William Reply

April 24 2017

I put in functions.php : function defer_parsing_of_js ( $url ) { if ( FALSE === strpos( $url, '.js' ) ) return $url; if ( strpos( $url, 'jquery.js' ) ) return $url; return "$url' defer "; } add_filter( 'clean_url', 'defer_parsing_of_js', 11, 1 ) It made my website crash. I had avada themes. Be careful.


    Domantas G.

    Replied on May 04 2017

    Hello, It may be different for Avada theme, their support should provide you the code to defer parsing.


    rakesh kumar

    Replied on January 07 2019

    Hi Basically this code loads in admin panel as well as front end. this is the reason your website crashed. It should load only on the fron-end. If you will load this only in front end then it should work.


Milius Reply

April 27 2017

Hi Sr can you make some array with this? I mean...I wanna exclude jquery. js and todo.js function defer_parsing_of_js ( $url ) { if ( FALSE === strpos( $url, '.js' ) ) return $url; if ( strpos( $url, 'jquery.js','todo.js' ) ) return $url; return "$url' defer='defer"; } add_filter( 'clean_url', 'defer_parsing_of_js', 11, 1 ); But not work


Jim Miesner Reply

May 02 2017

Hi. Thanks for this article it improved my page speed when I did this through the speed booster pack in wordpress, but then my background video didn't load. Is there a way to keep the background video active on my website and still defer parsing?


Antoine en road trip Reply

May 04 2017

It looks weird to put your code in function in wp-includes ? Are you sure it is not in function.php of the theme ?


    Domantas G.

    Replied on May 04 2017

    Hello, Yep, you are right. Fixed!


Webbing Barcelona Reply

June 21 2017

Not working properly any of this 3 plugins :/ not even the code applied on the functions.php. The main problem is the Home slider, it doesn't appear... Any other option to solve it? Thanks!


    Domantas G.

    Replied on June 23 2017

    Hey, Try to exclude your home slider JavaScript from deferring.


Amit Kumar Reply

January 06 2018

Hi Thanks for this article it improved my page speed when I did this through the WP deferred javaScript in wordpress.


Marco Reply

January 26 2018

Thank you. Very useful post!


Anuj Godara Reply

February 26 2018

Thanks for giving a good tips to us


Bob Reply

March 25 2018

That is amazing. I added the php code and the site went to A100% on both page speed and YSlow scores using GTmetrix. Before that it had been in the B/C range. I'd brought it up from D/C speeds by adjusting the served image sizes, but this code really made a difference. It seems to have fixed a number of other issues also, not just the defer parsing. I'm lazy so I tried the ASync plugin first, but that didn't appear to affect anything. Thank you!



    Replied on March 25 2018

    ok I need to retract that. The site wasn't available after I added the code. That could account for the increased speeds. Any suggestions?


Mike Reply

February 26 2019

Awesomeness!!! Keep up your excellent work! this code really works for me. Cheers, Mike


Baswoko Reply

March 02 2019

Hi, thank for the article, step 2 works on my site keep the good


Baswoko Reply

May 22 2019

two thumb up it's add speed to my website from 57 to 70 on google speed test on mobile Thank you


Caco Martín Reply

April 02 2020

Domantas I've tested the code in my website. WoW! 🚀 Thanks for share!


Ciara Sebecke Reply

June 02 2020

Thanks so much!! Just implemented this and immediately noticed a HUGE increase in site speed. I also appreciate you including the code to add without having to get the plugin.


Polycarp Opicha Reply

August 02 2020

Thanks so much can't imagine my grade changed from F 42 to C 80 in after installing speed booster and deferred J's Asante sana


    Tomislav T.

    Replied on September 06 2020

    Happy to help! Keep it up and you will get it to A! :)


Network Hope Reply

August 13 2020

I copied the code and pasted it in function.php. Earlier my score as per Gtmetric of "defer parsing of JS" was 44, which got improved to 46. Any further help? Thanks in advance. My domain is networkhope. in

Leave a reply




This site uses Akismet to reduce spam. Learn how your comment data is processed.

Become a part of Hostinger now!

More in WordPress