Skip to content

Instantly share code, notes, and snippets.

@evemilano
Last active August 9, 2018 14:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evemilano/a20419b800105c090c1e81d6ae701789 to your computer and use it in GitHub Desktop.
Save evemilano/a20419b800105c090c1e81d6ae701789 to your computer and use it in GitHub Desktop.
Add Defer & Async Attributes to many WordPress Script
function add_defer_attribute($tag, $handle) {
// add script handles to the array below
$scripts_to_defer = array('jquery-migrate', 'hoverIntent', 'superfish', 'superfish-args', 'skip-links', 'wp-embed');
foreach($scripts_to_defer as $defer_script) {
if ($defer_script === $handle) {
return str_replace(' src', ' defer="defer" src', $tag);
}
}
return $tag;
}
add_filter('script_loader_tag', 'add_defer_attribute', 10, 2);
//* If you want to use defer just replace async="async" with defer="defer".
//* You will need to change the handle which will be the first parameter of the enqueue method.
@evemilano
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment