How to Defer Parsing of JavaScript in WordPress

The most important one for a WordPress site is its speed and performance. So this is the reason why we should defer parsing of Javascript in wordpress.

So in order to load a page, the browser must parse the contents of all <script> tags, which adds additional time to the page load. This means that if you have lots of JS or <script> tags, it will take more time for the website’s content to appear, as it first waits for all the JavaScript to load.

By minimizing the amount of JavaScript needed to render the page and by defer parsing of JavaScript, the website would not wait for the JS code to load, which would result in a quicker loading time, so you can reduce the initial load time of your page.

When you check your website in the Pingdom, GTMetrix or Google’s Page insights, you will get “Defer loading of javascript” warning. If you get this warning, then you should follow the steps below.

How To Defer Parsing Javascript?

You can enable defer parsing for JavaScript just by adding ‘defer’ attribute. For example

How To Defer Parsing Javascript in WordPress?

To defer parsing of JS in WordPress, add the below code to the bottom of your theme’s functions.php file.

if (!(is_admin() )) {
    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 );

So we have learned how to improve the speed and performance of your website or WordPress powered website by deferring parsing of JavaScript. Remember, the faster and smoother your site is, the more traffic and happier visitors you will have!

Author: Dheeraj Dhawan

I'm a web developer / PHP programmer. I am also working on custom plugin development of wordpress.

Leave a Reply

Your email address will not be published. Required fields are marked *