Skip to content

Instantly share code, notes, and snippets.

@softiconic
Last active December 2, 2023 18:56
Show Gist options
  • Save softiconic/07a3cc4b12a815bbe9c47e7f3b6f42eb to your computer and use it in GitHub Desktop.
Save softiconic/07a3cc4b12a815bbe9c47e7f3b6f42eb to your computer and use it in GitHub Desktop.
Defer the JavaScript execution in the function file.
//defer js
function defer_parsing_of_js($url)
{
if (is_admin()) return $url;
if (false === strpos($url, '.js')) return $url;
if (strpos($url, 'jquery.js')) return $url;
if (strpos($url, ' js-mini.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