Last active
October 18, 2017 09:17
-
-
Save evemilano/038c9c65e475305065fa2ae88ae2a7c6 to your computer and use it in GitHub Desktop.
Add Defer & Async Attributes to a single WordPress Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Full article: : https://www.evemilano.com/2017/10/aggiungere-defer-async/