Skip to content

Instantly share code, notes, and snippets.

@walterebert
Last active March 12, 2018 10:44
Show Gist options
  • Select an option

  • Save walterebert/787d312c6cd8b6ffef03 to your computer and use it in GitHub Desktop.

Select an option

Save walterebert/787d312c6cd8b6ffef03 to your computer and use it in GitHub Desktop.
WordPress snippet: Load JavaScript asynchronously
function my_async_scripts( $tag, $handle, $src ) {
switch ( $handle ) {
case 'picturefill':
case 'wp-embed':
$tag = str_replace( ' src=', ' async defer src=', $tag );
break;
}
return $tag;
}
add_filter( 'script_loader_tag', 'my_async_scripts', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment