Skip to content

Instantly share code, notes, and snippets.

@jbd91
Last active June 8, 2020 19:52
Show Gist options
  • Save jbd91/5a8aee079c43df8756772dd03fcffcdb to your computer and use it in GitHub Desktop.
Save jbd91/5a8aee079c43df8756772dd03fcffcdb to your computer and use it in GitHub Desktop.
async
function add_async_forscript($url)
{
if (strpos($url, '#asyncload')===false)
return $url;
else if (is_admin())
return str_replace('#asyncload', '', $url);
else
return str_replace('#asyncload', '', $url)."' async='async";
}
add_filter('clean_url', 'add_async_forscript', 11, 1);
// BEST METHOD
// Make JavaScript Asynchronous in Wordpress
add_filter( 'script_loader_tag', function ( $tag, $handle ) {
if( is_admin() ) {
return $tag;
}
return str_replace( ' src', ' async src', $tag );
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment