Skip to content

Instantly share code, notes, and snippets.

@Asikur22
Forked from alexander-young/functions.php
Last active March 21, 2022 02:24
Show Gist options
  • Save Asikur22/1a5dc964f5c7aa7e787a2b44f3951249 to your computer and use it in GitHub Desktop.
Save Asikur22/1a5dc964f5c7aa7e787a2b44f3951249 to your computer and use it in GitHub Desktop.
Defer Javascript/CSS in WordPress
function defer_parsing_of_js($url)
{
if (is_admin()) 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);
function defer_parsing_of_css( $url ) {
if ( strpos( $url, 'dist/leaflet.css' ) ) {
return str_replace( ' href', ' async defer href', $url );
}
return $url;
}
add_filter( 'style_loader_tag', 'defer_parsing_of_css', 99 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment