Last active
February 11, 2019 02:34
-
-
Save ericakfranz/025e1fb3e07c24a49d30 to your computer and use it in GitHub Desktop.
Defer Parsing of JavaScripts for WordPress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//* 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 ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//* 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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@TechMagy,
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.
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 theonload='
matched the single-quotes and fixed the defer issue, but I believe the 'onload' attribute is completely unnecessary, and the correct line isreturn "$url' defer='defer";
.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.