Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
function smartwp_defer_js_parsing( $url ){
if(is_admin()) return $url; //Skip admin JS files
if(is_user_logged_in()) return $url; //Skip if user is logged in
if(false === strpos($url, '.js')) return $url; //If it's not a JS file skip
if(strpos($url, 'jquery.js')) return $url; //Don't defer jQuery
return str_replace(' src', ' defer src', $url); //defer JS file
}
add_filter( 'script_loader_tag', 'smartwp_defer_js_parsing', 10 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment