async vs defer attributesTagsTheasyncanddeferattributes for the
<script> <script async> <script defer> async. defer. async script then use an inlinescript with no attributes placed above the async scripts. add_defer_attribute function ($ 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) {
if ($ defer_script === $ handle) {
return str_replace ( 'src', 'defer = "defer" src ", $ tags);
}
}
return $ tag;
} add_filter ( 'script_loader_tag', 'add_defer_attribute', 10, 2);
add_async_attribute function ($ tag, $ handle) {
// add script handles to the array below
$ Scripts_to_async = array ( 'my-js-handle', 'another-handle');
foreach ($ scripts_to_async as $ async_script) {
if ($ async_script === $ handle) {
return str_replace ( 'src', 'async = "async" src ", $ tags);
}
}
return $ tag;
} add_filter ( 'script_loader_tag', 'add_async_attribute', 10, 2);