Skip to content

Instantly share code, notes, and snippets.

@nydame
Last active February 19, 2019 23:01
Show Gist options
  • Save nydame/d189f3412cfb9404a307ac1d4a5c6687 to your computer and use it in GitHub Desktop.
Save nydame/d189f3412cfb9404a307ac1d4a5c6687 to your computer and use it in GitHub Desktop.
WordPress filter that will add "async" to an enqueued script
#Assuming the script has already been enqueued with 'my-script' as a handle, add this to functions.php or to a plugin
add_filter( 'script_loader_tag', 'add_async_to_my_script', 10, 3 );
function add_async_to_my_script( $tag, $handle, $src ) {
if ( $handle === 'my_script') {
$tag = '<script type="text/javascript" scr="' . esc_url($src) . '" async></script>';
}
return $tag;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment