defer to the
<script> tag. Here is a technique to do it with any WordPress version 4.1 and better.
Here is the magic function:
async attribute; if not found then it is added. Likewise the second condition checks if the
defer attribute is present; if not, then it is added. All of this happens by hooking the function into the script_loader_tag filter hook.
Customize as needed to add whichever attributes are necessary. Remember to change the
About async & defer
Also for reference, here is what the async and defer attributes are doing:
- async — Asynchronous tells the browser to execute the script when ready, without blocking any HTML parsing
- defer — Deferred tells the browser to delay script execution until HTML parsing is complete
Remember to test thoroughly by viewing the actual HTML output of your pages (via View Source or Inspector et al).