Make Your Site Faster with Async and Deferred JavaScript: Introducing script_loader_tag

Today I’m going to discuss a new, and very nice, site speed improvement that became possible in WordPress 4.1. What changed? The introduction of a new filter, script_loader_tag. This filter lets us easily change the HTML markup of enqueued script elements—that is, of JavaScript files that were correctly added into a WordPress site using WordPress’s wp_enqueue_script function.

With script_loader_tag, we can now easily fix a problem that can significantly impact page speed: lots of render-blocking JavaScript.

The Problem: Render-Blocking JavaScript

Long JavaScript files in your head can delay your browser from displaying page content, because its default behavior is first to interpret the JS files themselves.

Properly enqueued JavaScript shows up in the head section of your HTML document. On the internet as in nature, the main thing about a head is that it’s above a body—and this means something fairly serious for site speed, because JavaScript can be render-blocking.

“Render-blocking” comes from a web browser’s default behavior: It wants to completely receive and process everything that’s come higher up in the page, before it moves any further down.

This means that long JavaScript files in your head can actually delay your browser from displaying the page content in the body, because its default behavior is first to interpret the JS files themselves. In other words, JS is blocking the browser’s crucial function of rendering the page out for the user to actually see. The result can be slow sites and frustrated users.

.. The Fix: Defer and Async your JavaScript

The first thing to understand is the alternatives to render-blocking JS: defer and async. We’ll explain the difference, but both work similarly: They let the browser load a JS resource “as time permits,” while attending to other things (like page rendering) as well. This means that you can’t rely on a deferred or asynced JavaScript file being in place prior to page render, as you could without these attributes—but the advantage is that the file won’t slow the speed at which the page becomes visible to users.

Those are concepts—now for code. (The full code is available on GitHub.)


.. 2. Deferring and Asyncing Render-Blocking JavaScript

We found that we needed to use defer and not async for WPShout, so I’ll walk through the defer code. Most of the heavy lifting here is from an article by Scott Nelle; thanks, Scott!

add_filter( 'script_loader_tag', 'wsds_defer_scripts', 10, 3 );
function wsds_defer_scripts( $tag, $handle, $src ) {

	// The handles of the enqueued scripts we want to defer
	$defer_scripts = array( 

    if ( in_array( $handle, $defer_scripts ) ) {
        return '<script src="' . $src . '" defer="defer" type="text/javascript"></script>' . "\n";
    return $tag;

The add_filter line tells us that this code should run anytime an enqueued JavaScript file is about to be printed onto the page as an HTML script element. Letting us filter that HTML is what script_loader_tag is for. (If you need an update on filters and WordPress’s hooks system in general, start here!)


GitHub project:


How To Fix Defer Parsing of Javascript in WordPress (5 Ways)

As somebody who is trying to understand how to defer parsing of Javascript, we believe you have a problem. You own or have visited a website of somebody you know (maybe a client?) and it takes forever to load. And when you ran it through website speed testing tools, you got a recommendation which you’re not sure how to implement.

In this article, we’re going to share several ways of how to fix this warning and ultimately, make the website load faster, without throwing this error!

If you’re short on time, here are some quick actions you can do:

Instructions for How To Defer Parsing of JavaScript

  1. Download the Async plugin here.
  2. Click on Plugins > Add New > Upload plugin and select the file you have just downloaded.
  3. Click on Activate of the installed plugin.
  4. Go to Plugins and click on Settings for the Async plugin you have just installed.
  5. Clicking on Enable Async Javascript, or Apply Async as two of the most common ways to apply the fix.
  6. Test your website, to see that everything is still working well.



Getting Started with Sphinx (video)

Sphinx is a powerful documentation generator that has many great features for writing technical documentation including:

  • Generate web pages, printable PDFs, documents for e-readers (ePub), and more all from the same sources
  • You can use reStructuredText or Markdown to write documentation
  • An extensive system of cross-referencing code and documentation
  • Syntax highlighted code samples
  • A vibrant ecosystem of first and third-party extensions

Quick start video

This screencast will help you get started or you can read our guide below.