Skip to content

Instantly share code, notes, and snippets.

@bublick
Forked from alexander-young/functions.php
Created July 18, 2020 17:54
Show Gist options
  • Save bublick/7f3455c3668b6a91a5b8058b2c6bdc90 to your computer and use it in GitHub Desktop.
Save bublick/7f3455c3668b6a91a5b8058b2c6bdc90 to your computer and use it in GitHub Desktop.
Defer Javascript 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment