PHIL KURTH

Email

How to set defer or async attributes on enqueued script tags in WordPress

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;

One last thing…

If you liked this article, I'd be incredibly grateful if you tweeted about it.

Also, I don't send emails often but when I do, I try to fill them full of useful goodies. If you like code snippets & dev tips, join my mailing list. There's no catch but I'll probably want to tell you about any new plugins & tools I build.