Skip to content

Instantly share code, notes, and snippets.

@davidsword
Last active March 18, 2019 16:19
Show Gist options
  • Save davidsword/2da87a154ace68c7537b1d10c57348df to your computer and use it in GitHub Desktop.
Save davidsword/2da87a154ace68c7537b1d10c57348df to your computer and use it in GitHub Desktop.
WordPress - Make `wp_enqueue`'d scripts async.
<?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