Skip to content

Instantly share code, notes, and snippets.

@allenmoore
Last active September 6, 2016 14:23
Show Gist options
  • Save allenmoore/b0877a7e637a52ff9815 to your computer and use it in GitHub Desktop.
Save allenmoore/b0877a7e637a52ff9815 to your computer and use it in GitHub Desktop.
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