Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Async an arrayed list of JavaScript files with script_loader_tag
/**
* Function to Async an arrayed list of JavaScript files.
*
* @param $tag
* @param $handler
* @param $src
*
* @return mixed
*/
function async_scripts( $tag, $handler, $src ) {
/**
* An array of JavaScripts files. The value should be the filename.
*/
$scripts = array(
'script_one.js',
'script_two.js'
);
/**
* Loop through each file in the $scripts array and add an async option.
*/
foreach ( $scripts as $script ) {
if ( true == strpos( $tag, $script ) )
return str_replace( ' src', ' async src', $tag );
}
return $tag;
}
add_filter( 'script_loader_tag', 'async_scripts', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.