Last active
September 6, 2016 14:23
-
-
Save allenmoore/b0877a7e637a52ff9815 to your computer and use it in GitHub Desktop.
Async an arrayed list of JavaScript files with script_loader_tag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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