Last active
March 18, 2019 16:19
-
-
Save davidsword/2da87a154ace68c7537b1d10c57348df to your computer and use it in GitHub Desktop.
WordPress - Make `wp_enqueue`'d scripts async.
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
<?php | |
/** | |
* Set all enqueued scripts except jQuery to include the `async` attribute. | |
* | |
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#Attributes | |
*/ | |
add_filter('script_loader_tag', function ($tag, $handle) { | |
if ( 'jquery' !== $handle ) | |
return $tag; | |
return str_replace( ' src', ' async="async" src', $tag ); | |
}, 99, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment