Reputation: 2728
I'm working on a wordpress theme I added a plugin name wp-fastest-cache which execute a script tag on head section
<script src='//mysite.com/wp-content/cache/wpfc-minified/fad3/13d93index.js' type='text/javascript'></script>
I have tried lots of trick from stack overflow and google but failed.Lots of files I tried to edit like function.php, fucntion.wp-script.php etc. still stuck. I need to add async = 'async' like
<script async = 'async' src='//mysite.com/wp-content/cache/wpfc-minified/fad3/13d93index.js' type='text/javascript'></script>
I know there are lots of plugins. But I want to do it manually.
And sorry for my bad english.
Thanks,
Upvotes: 0
Views: 1082
Reputation: 21
function add_defer_attribute($tag, $handle) {
// add script handles to the array below
$scripts_to_defer = array('gform_recaptcha');
foreach ($scripts_to_defer as $defer_script) {
if ($defer_script === $handle) {
return str_replace(' src', ' async src', $tag);
}
}
return $tag;
}
add_filter('script_loader_tag', 'add_defer_attribute', 10, 2);
// Place above code in function file. you can add multiple id after comma. For Ex. ('gform_recaptcha', 'another_id');
Upvotes: 0
Reputation: 6650
Please use the following code inside functions.php to add async
to the javascript.
if(!is_admin()){
function defer_async_of_js ( $url ) {
if ( strpos( $url, 'jquery.js' ) ) return "$url' ";
if ( strpos( $url, '.js' ) ) {
return "$url' async='async' ";
}
else
{
return $url;
}
}
add_filter( 'clean_url', 'defer_async_of_js', 11, 1 );
}
Upvotes: 2