Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save addzycullen/332b2831c72685b365b07811d3a2cdba to your computer and use it in GitHub Desktop.
Save addzycullen/332b2831c72685b365b07811d3a2cdba to your computer and use it in GitHub Desktop.
/**
* Add Async & Defer to certain scripts.
*
* @param string $tag Script Tags to display.
* @param string $handle Name of file specified above.
*/
function add_async_defer_attribute( $tag, $handle ) {
$scripts = array( 'script-handle' );
foreach ( $scripts as $script ) {
if ( $script === $handle ) {
return str_replace( ' src', ' async="async" defer="defer" src', $tag );
}
}
return $tag;
}
add_filter( 'script_loader_tag', 'add_defer_attribute', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment