Skip to content

WordPress JS optimization: defer, async, minify — what each does and when

WordPress JavaScript optimization that goes beyond "enable everything" — defer vs async vs delay-execution, how to apply each without breaking themes, and the WP_Scripts API for surgical control.

A WordPress page that ships 1.4 MB of JavaScript in the first viewport will fail INP and TBT no matter how good its other optimizations are. Most of the JS is not needed for first paint — but turning it all off via “delay JavaScript execution” breaks forms, analytics, and chat widgets. The win comes from knowing which technique applies to which script.

What’s happening

Four techniques affect JavaScript loading and execution on a WordPress site:

  • async: script downloads in parallel with HTML parsing, executes as soon as it finishes downloading. Order is not guaranteed. Good for analytics and tags that do not depend on each other.
  • defer: script downloads in parallel, but execution is deferred until after the HTML is parsed. Order is preserved. Good for scripts that depend on the DOM being ready, like theme JS and slider plugins.
  • Delay execution until user interaction (WP Rocket “Delay JavaScript Execution”, Flying Scripts): script is not loaded at all until the user clicks or scrolls. Drastic but effective for third-party tags that are not needed for the initial experience.
  • Minify: reduces script size by removing whitespace and comments. The benefit on modern HTTP/2 with gzip or Brotli is small but real. Combined files reduce request count but break long-tail caching when one file changes.

A WordPress site that applies “delay execution” to its theme’s jQuery dependency will break every script that depends on jQuery. A site that applies async to a script that loads after its dependent script will break load order. Each technique needs a per-script decision.

How to verify

Inventory current script loading:

curl -s https://www.example.com/ \
  | grep -oE '<script[^>]*>' | head -50

You will see a mix of <script src="..." defer>, <script src="..." async>, plain <script src="...">, and inline <script> blocks. Identify what is loading in each pattern.

Find the dependency chain:

wp shell <<< 'global $wp_scripts; print_r($wp_scripts->registered);' | head -100

This dumps every registered script with its dependencies. Look for the scripts that have many dependents — those need defer, not delay-execution.

Measure the impact in Chrome DevTools:

# Performance panel → record a page load
# Look at the "Scripting" total time at the bottom
# Sort by self time to find the heaviest scripts

The fix

  1. Defer all theme and plugin scripts that depend on the DOM: add the defer attribute via the script_loader_tag filter:
add_filter('script_loader_tag', function($tag, $handle) {
    $defer_handles = ['my-theme-js', 'slider-plugin', 'gallery-script'];
    if (in_array($handle, $defer_handles, true)) {
        return str_replace(' src=', ' defer src=', $tag);
    }
    return $tag;
}, 10, 2);
  1. Async analytics and pixel scripts: Google Analytics, Facebook Pixel, Hotjar — these have no dependencies and can run any time. WordPress plugins for these usually inject async already; verify in the HTML.
  2. Delay execution for third-party tags that load on interaction: chatbots, intercom widgets, optional analytics that fire on first scroll. Use WP Rocket’s “Delay JavaScript Execution” with an exclusion list to keep core functionality (forms, navigation) intact.
  3. Minify in the build, not at request time: if your theme has a build step (webpack, Vite, Laravel Mix), minify there. If not, enable the cache plugin’s minify but exclude any script that loads from a CDN at a fixed URL (the plugin will rewrite the URL and break it).
  4. Audit jQuery dependency: WordPress core ships jQuery 3.x. Theme code that depends on jQuery cannot use delay execution on jQuery itself. If you can rewrite to vanilla JS, jQuery becomes another script to defer or remove entirely.
  5. Do not concatenate files on HTTP/2 or HTTP/3: combining files into one was a hack for HTTP/1.1 connection limits. HTTP/2 and HTTP/3 multiplex, so combining hurts long-tail caching. Disable WP Rocket’s “Combine JavaScript” on modern hosting.

After each change, re-measure TBT (Total Blocking Time) and INP. The expected improvement from getting defer right on a heavy WordPress page is 200-500 ms TBT.

Stack Harbor manages per-script optimization decisions across Enterprise WordPress accounts with a maintained exclusion list per template. If your delay-execution setting broke a form, book a 30-minute review.