Skip to content

Instantly share code, notes, and snippets.

@ericakfranz
Last active February 11, 2019 02:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericakfranz/025e1fb3e07c24a49d30 to your computer and use it in GitHub Desktop.
Save ericakfranz/025e1fb3e07c24a49d30 to your computer and use it in GitHub Desktop.
Defer Parsing of JavaScripts for WordPress
//* Defer parsing of JavaScripts, but exclude OptinMonster!
function defer_parsing_of_js ( $url ) {
if ( FALSE === strpos( $url, '.js' ) ) return $url;
if ( strpos( $url, 'jquery.js' ) || strpos( $url, 'optinmonster' ) ) return $url;
return "$url' defer='defer";
}
add_filter( 'clean_url', 'defer_parsing_of_js', 11, 1 );
//* Defer parsing of JavaScripts
function defer_parsing_of_js ( $url ) {
if ( FALSE === strpos( $url, '.js' ) ) return $url;
if ( strpos( $url, 'jquery.js' ) ) return $url;
return "$url' defer='defer";
}
add_filter( 'clean_url', 'defer_parsing_of_js', 11, 1 );
@uhlhosting
Copy link

How does one use these?

@TechMagy
Copy link

TechMagy commented Aug 8, 2018

I have read that this is best pasted into your 'functions.php' file. I have also seen a variation on this code as:

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 ";
return "$url' defer onload='";
}
add_filter( 'clean_url', 'defer_parsing_of_js', 11, 1 );
}

I would be curious to hear if others here can see any issues with this code, or it being added to functions.php

@mwordpress
Copy link

add jquery.min.js

@ronezone
Copy link

@TechMagy,

if (!(is_admin() )) {

The above line prevents problems with certain plugins when logged-in to the WP Dashboard (admin interface.) If the user viewing the website is logged-in to the admin interface, there's no need to defer. This code is important to include; especially for novice users who, for instance, don't understand why they're excluding 'jquery.js.' Without this code, novices can get locked out of their site.

function defer_parsing_of_js ( $url ) {
if ( FALSE === strpos( $url, '.js' ) ) return $url;
if ( strpos( $url, 'jquery.js' ) ) return $url;
// return "$url' defer ";
return "$url' defer onload='";

I suspect the above line evolved from an invalid post that resulted in malformed HTML, an unmatched single-quote, (return "$url' defer ";,) which caused the defer attribute to be ignored... inserting the onload=' matched the single-quotes and fixed the defer issue, but I believe the 'onload' attribute is completely unnecessary, and the correct line is return "$url' defer='defer";.

}
add_filter( 'clean_url', 'defer_parsing_of_js', 11, 1 );
}

I would be curious to hear if others here can see any issues with this code, or it being added to functions.php

I've been noticing an increasing number of bloggers appear to be copying 'solutions' from other web sites and posting them as their own; often without testing. This is a sad trend, which has resulted in garbage bubbling-up to the top of Google SERP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment