How to set defer or async attributes on enqueued script tags in WordPress
Last updated • October 16th, 2019
If you get down to business with performance optimisation, you could find yourself looking at ways to defer your scripts or even load them asynchronously.
As far as I’m aware, WordPress’ built-in functions wp_register_script() and wp_enqueue_script() don’t currently take arguments for defining these attributes. But, with WordPress being WordPress, there is usually a way and in this case, we just need to hook into the script_loader_tag filter and modify the HTML directly.
Here is a snippet to get you moving;
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters