wordpress script_loader_tag in function.php
Solution 1:
Replace
$scripts_to_defer = array('jquery-core-js','fortuna.lib-js');
With this
$scripts_to_defer = array( 'jquery-core', 'fortuna.lib' );
You don't need to add -js
to handle because WordPress automatically adds when enqueue scripts Try the below code.
function add_defer_attribute( $tag, $handle ) {
// add script handles to the array below
$scripts_to_defer = array( 'jquery-core','fortuna.lib' );
foreach( $scripts_to_defer as $defer_script ) {
if ($defer_script === $handle) {
return str_replace( ' src', ' defer="defer" src', $tag );
}
}
return $tag;
}
add_filter( 'script_loader_tag', 'add_defer_attribute', 10, 2 );
function add_async_attribute( $tag, $handle ) {
// add script handles to the array below
$scripts_to_async = array( 'jquery-core', 'fortuna.lib' );
foreach( $scripts_to_async as $async_script ) {
if ($async_script === $handle) {
return str_replace( ' src', ' async="async" src', $tag );
}
}
return $tag;
}
add_filter( 'script_loader_tag', 'add_async_attribute', 10, 2 );
Tested and working fine.