jQuery, Tutorials, Wordpress

How to Defer Parsing of JavaScript in WordPress

09 July 2018

Here we will see How to Defer Parsing of JavaScript in WordPress.

There are a few reasons why we should defer parsing of JavaScript in WordPress. The most important one is speed and performance. Generally, JavaScript is placed between the head tags. When opening a website, it loads the code from top to bottom. This means that if you have lots of JS or long strings, it will take more time for the website’s content to appear, as it first waits for all the JavaScript to load.

Put the below code in function.php file

Put the below code at bottom of your function.php file of Active theme of site. The function.php file is location in root directory of theme 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 );

Leave a Comments