How do I defer or async this WordPress javascript snippet to load lastly for faster page load times?

How do I defer or async this WordPress javascript snippet to load lastly for faster page load times?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Defer and Async in WordPress: Controlling JavaScript Load Order

You are correctly identifying that loading non-critical JavaScript files asynchronously or defer-rending them is a core strategy for optimizing Core Web Vitals, specifically the Largest Contentful Paint (LCP) metric. The confusion stems from the fact that you have access to PHP hooks to enqueue scripts, but you cannot directly pass HTML attributes like defer or async to wp_enqueue_script(). These attributes belong in the <script> tag generated by the browser, not the PHP string passed to the queue function.

To achieve this goal, you must use WordPress's wp_script_attributes filter. This hook allows you to intercept the default attributes assigned to a script handle and override them with your custom configuration.

Implementing Deferral via Filters

The standard approach for deferring scripts is to pass a third argument to wp_enqueue_script() containing an associative array of attributes. However, this only applies if WordPress detects that the script is not in the header or footer context where it defaults to loading synchronously. A more robust method involves using the filter hook directly on the specific handle you are enqueueing.

Here is how you modify your plugin file:

function add_dcsnt_scripts() {
    wp_enqueue_script( 'jquery' );

    // Enqueue the script first, then apply attributes
    wp_enqueue_script( 
        'dcsnt', 
        dc_jqsocialtabs::get_plugin_directory() . '/js/jquery.social.media.tabs.1.7.1.min.js',
        array(), // Dependencies (optional)
        null,    // Version (optional)
        true     // In footer: This is crucial for defer/async behavior
    );

    // Apply 'defer' attribute to the specific script handle
    wp_script_attributes( 'dcsnt', array('defer') );
}
add_action( 'wp_enqueue_scripts', 'add_dcsnt_scripts' );

The key here is the third argument passed to wp_script_attributes. By passing array('defer'), you instruct the browser to download the script asynchronously but execute it once the HTML parsing is complete. This ensures your critical rendering path remains unblocked by external JavaScript.

The Distinction Between Async and Defer

It is vital to understand the difference between these two attributes, as they do not behave identically in all contexts.

async loads the file in parallel with page parsing but executes the script immediately once it finishes downloading. If your plugin's JavaScript relies on DOM elements or jQuery objects initialized by other scripts, async might cause race conditions where your code runs before the DOM is ready, resulting in errors.

defer, conversely, loads the file in parallel but delays execution until the HTML document has been completely parsed and the DOM is ready. This makes it generally safer for plugins that manipulate the DOM, such as social media tabs or sliders, because you don't have to worry about timing issues with other scripts.

Managing Dependencies and Footer Loading

Your original code snippet includes wp_enqueue_script( 'jquery' ); before your custom script. If you use async or defer, you must ensure that jQuery is also loaded before your plugin's script attempts to use it. With the defer attribute, the browser respects the order of scripts in the source code relative to the <head> tag, even if they are all deferred. However, if you move everything to the footer (which wp_enqueue_scripts does by default), the order is preserved as long as you enqueue jQuery first and your plugin second.

If you decide to apply these attributes globally to all scripts in a specific theme or plugin, you can create a wrapper function:

function defer_all_scripts( $handle ) {
    if ( ! wp_script_is( $handle, 'enqueued' ) ) {
        return;
    }
    
    // Check if the script is already deferred or async to avoid duplicates
    $current_attrs = wp_scripts()->get( $handle );
    if ( isset( $current_attrs->deps ) ) {
        // Logic to ensure dependencies are met before deferring
    }
}

However, for most plugin developers, applying the filter individually per script is cleaner and less error-prone.

Why This Improves Page Load Times

By moving these scripts to the bottom of the load chain using defer, you prevent the browser from being blocked on parsing non-essential JavaScript while it renders your content. Speed testers flag synchronous JavaScript execution in the critical path as a bottleneck because the browser must pause rendering to execute that code.

Using wp_script_attributes with the defer value ensures that the browser treats your plugin's file as a background download task until the structure of the page is ready. This maintains a smooth user experience, particularly on mobile networks where latency can be high.

While resources like LaravelCompany discuss similar optimization strategies in PHP frameworks, the principle remains identical: control the execution timing of external assets to prioritize content delivery over script evaluation. In WordPress, the wp_script_attributes filter is the standardized mechanism to enforce this behavior without modifying the browser's native behavior or using third-party plugins that might conflict with your existing setup.

Our network