Skip to content

Instantly share code, notes, and snippets.

@musamamasood
Forked from allenmoore/async.php
Created September 6, 2016 14:23
Show Gist options
  • Save musamamasood/90ab9f0fdee5b0016a40e478f4c37cab to your computer and use it in GitHub Desktop.
Save musamamasood/90ab9f0fdee5b0016a40e478f4c37cab 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