Adding async tag to scripts in function.php

  • 0

Add below code in fucntion.php

function add_defer_attribute($tag, $handle) {
// add script handles to the array below
$scripts_to_defer = array(‘my-js-handle’, ‘another-handle’);
foreach($scripts_to_defer as $defer_script) {
return str_replace(‘ src’, ‘ async=”async” src’, $tag);
}
return $tag;
}
add_filter(‘script_loader_tag’, ‘add_defer_attribute’, 10, 2);

Reference :http://matthewhorne.me/defer-async-wordpress-scripts

Add below code in fucntion.php function add_defer_attribute($tag, $handle) { // add script handles to the array below $scripts_to_defer = array(‘my-js-handle’, ‘another-handle’); foreach($scripts_to_defer as $defer_script) { return str_replace(‘ src’, ‘ async=”async” src’, $tag); } return $tag; } add_filter(‘script_loader_tag’, ‘add_defer_attribute’, 10, 2); Reference :http://matthewhorne.me/defer-async-wordpress-scripts

Add below code in fucntion.php function add_defer_attribute($tag, $handle) { // add script handles to the array below $scripts_to_defer = array(‘my-js-handle’, ‘another-handle’); foreach($scripts_to_defer as $defer_script) { return str_replace(‘ src’, ‘ async=”async” src’, $tag); } return $tag; } add_filter(‘script_loader_tag’, ‘add_defer_attribute’, 10, 2); Reference :http://matthewhorne.me/defer-async-wordpress-scripts