Skip to content

Instantly share code, notes, and snippets.

@developer-anuragsingh
Created August 8, 2022 12:31
Show Gist options
  • Save developer-anuragsingh/0029d70594cca72a1189aa82f9dccf16 to your computer and use it in GitHub Desktop.
Save developer-anuragsingh/0029d70594cca72a1189aa82f9dccf16 to your computer and use it in GitHub Desktop.
Add defer in script calls
function defer_parsing_of_js( $url ) {
if ( is_user_logged_in() ) return $url; //don't break WP Admin
if ( FALSE === strpos( $url, '.js' ) ) return $url;
if ( strpos( $url, 'jquery.js' ) ) return $url;
return str_replace( ' src', ' defer src', $url );
}
add_filter( 'script_loader_tag', 'defer_parsing_of_js', 10 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment