Skip to content

Instantly share code, notes, and snippets.

@evemilano
Last active October 18, 2017 09:17
Show Gist options
  • Save evemilano/038c9c65e475305065fa2ae88ae2a7c6 to your computer and use it in GitHub Desktop.
Save evemilano/038c9c65e475305065fa2ae88ae2a7c6 to your computer and use it in GitHub Desktop.
Add Defer & Async Attributes to a single WordPress Script
function add_async_attribute($tag, $handle) {
if ( 'my-js-handle' !== $handle )
return $tag;
return str_replace( ' src', ' async="async" src', $tag );
}
add_filter('script_loader_tag', 'add_async_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