Speed & performance · Guide
How to Defer and Delay JavaScript in WordPress
Short answer
To defer JavaScript in WordPress, mark scripts with the defer strategy so the browser downloads them in the background and runs them after the page is parsed. WordPress 6.3 added a native strategy argument to wp_enqueue_script, and plugins such as WP Rocket add the same switch. Delaying goes further, holding scripts until the visitor interacts with the page.
Key takeaways
- A normal script tag stops the browser building the page until the script runs.
- Deferred scripts run after parsing, in order; async scripts run as soon as they arrive, in no set order.
- WordPress 6.3 added a native strategy argument to wp_enqueue_script for defer and async.
- Delaying holds scripts until the visitor interacts, which clears more warnings than defer alone.
- Never defer jQuery or scripts the first screen needs, and always test menus, forms and checkout after a change.
What does it mean to defer JavaScript?
Deferring JavaScript means telling the browser to download a script in the background and run it after the page has been parsed.
A normal script tag stops the browser. When the browser meets a script without defer or async, it pauses building the page, downloads the script, runs it and only then carries on. Web.dev explains the cost plainly: JavaScript execution is parser-blocking, so the page cannot appear until the script finishes.
Deferring breaks that pause. The browser keeps building the page while the script downloads, then runs the script once parsing is done. Visitors see content sooner, and scripts that the first screen does not need wait their turn.
On WordPress, deferring is a loading strategy, not a file edit. You mark a script as deferred when it is enqueued, or a plugin rewrites the script tag for you. The file itself does not change, and the script still runs, just later.
How do defer, async and delay differ?
Defer, async and delay all load scripts later, but they run them at different times and in a different order.
A deferred script downloads in the background and runs after the HTML is fully parsed, before the DOMContentLoaded event.
Deferred scripts run in the order they appear in the page, according to the WordPress developer reference for wp_enqueue_script. That order is what makes defer safe for scripts that depend on each other.
An async script also downloads in the background, but it runs the moment it finishes downloading.
The same reference warns that async scripts have no guaranteed order: script B can run before script A if it loads first.
Web.dev recommends async for independent scripts such as analytics, and defer for less critical resources such as videos below the fold.
Delay is stronger than both. WP Rocket's Delay JavaScript Execution waits until the visitor interacts with the page, such as moving the mouse, touching the screen, scrolling or pressing a key. Nothing runs until that first interaction, which is why delay clears more render-blocking warnings than defer alone.
| Method | Downloads | Runs | Keeps order |
|---|---|---|---|
| Normal script | Blocks parsing | At once, in place | Yes |
| defer | In the background | After parsing, before DOMContentLoaded | Yes |
| async | In the background | As soon as downloaded | No |
| Delay until interaction | In the background | After first user interaction | Yes, as loaded |
How do you find the scripts to defer?
Run PageSpeed Insights first, and let its report name the files.
Open PageSpeed Insights, enter the page address and run the audit. The report lists render-blocking requests with the file names, sizes and the time each one costs. That list is your work list: theme scripts, jQuery, a slider, a form plugin and analytics usually sit at the top.
WordPress has supported deferred loading natively since version 6.3, through the script loading strategy in wp_enqueue_script.
That means scripts your theme or a plugin enqueues can carry a strategy, while scripts printed by hand in a template cannot. Check the page source to see which scripts come from enqueued handles and which are hard-coded.
Sort the list before you touch anything.
Scripts the first screen needs, such as a menu script or a consent banner, stay as they are. Scripts for a slider below the fold, a chat widget or a map can wait.
Removing a script the page does not need beats deferring it, so delete dead plugins first.
Unused JavaScript is a separate problem from slow JavaScript, and it needs its own fix.
How do you defer JavaScript with WordPress core?
Use the strategy argument when you enqueue a script, and WordPress adds the attribute for you.
WordPress 6.3 added support for registering scripts with async and defer attributes, according to the Make WordPress Core announcement. The fifth argument of wp_enqueue_script now accepts an array with a strategy key and an in_footer key. Here is how a theme defers its own script:
add_action( 'wp_enqueue_scripts', function() {
wp_enqueue_script(
'my-theme-main',
get_theme_file_uri( 'assets/js/main.js' ),
array(),
'1.0.0',
array(
'strategy' => 'defer',
'in_footer' => true,
)
);
} );
WordPress also guards the order for you. The Make WordPress Core post explains that if a deferred script has a dependent registered as blocking, WordPress downgrades the deferred one to blocking so the order holds. If a handle has an inline script attached after it, WordPress drops the strategy and treats it as blocking.
To defer a script a plugin already registered, use wp_script_add_data after the plugin enqueues it:
add_action( 'wp_enqueue_scripts', function() {
wp_script_add_data( 'some-plugin-script', 'strategy', 'defer' );
}, 20 );
The priority of 20 matters. It runs after the plugin's own enqueue at the default priority of 10, so the handle exists when your code runs. Find the handle in the page source: it is the script tag id without the -js suffix.
How do you defer JavaScript with a plugin?
A performance plugin defers scripts with a switch, and it is the fastest route for most sites.
WP Rocket has a “Load JavaScript deferred” option on its File Optimization tab.
Switching it on adds the defer attribute to the JavaScript files on the page, according to WP Rocket's own guide to deferring JavaScript.
The same guide notes that Async JavaScript, a free plugin, gives per-script control over async and defer, and that Asset CleanUp can defer combined files.
LiteSpeed Cache and other caching plugins offer the same kind of switch under their optimization settings. If your host runs LiteSpeed, use its cache plugin before adding a second optimizer, since two plugins rewriting the same script tags can fight each other.
Do not stack plugins that do the same job. One deferral switch is enough, and a second one adds risk without adding speed. Pick the plugin you already use for caching, turn on its defer option, and test the site before you touch anything else.
WP Rocket has a dedicated setting for delaying scripts, and its exclusions list is worth knowing.
How do you delay JavaScript until interaction?
Delaying holds every script back until the visitor moves, scrolls or taps, which clears the heaviest warnings.
WP Rocket's Delay JavaScript Execution is the most widely used version of this.
Its documentation explains that it delays all JavaScript files and inline scripts until user interaction, such as moving the mouse, touching the screen, scrolling or pressing a key.
It applies only on cached or optimized pages, and it turns off the Combine JavaScript files feature so scripts keep their order.
Turn it on under “Settings” then “WP Rocket”, on the File Optimization tab. After enabling it, open the site and watch what happens: only one script loads at first, and the rest load after the first mouse movement. That pause is the feature working, not a bug.
WP Rocket's own documentation warns that the delay can hide elements until interaction.
A slider, a chat box or a cookie banner can appear late, or sit blank until the visitor moves the mouse.
When that happens, exclude the affected script from the delay instead of switching the whole feature off. The docs describe two ways: add the nowprocket attribute to the script tag, or use the Safe Mode option while you find the exact files.
Core Web Vitals scores move when scripts load late, so know what each metric measures.
Which scripts should you never defer?
Never defer jQuery, scripts that run inline code straight after them, or scripts the first screen needs.
Web.dev warns against using async for essential libraries such as jQuery, and the same caution applies to defer. Many themes and plugins run inline code that expects jQuery to exist already. If jQuery loads late and the inline code runs first, menus, sliders and forms break with errors in the console.
Inline scripts are the second trap. WordPress drops the defer strategy when a handle has an inline script attached after it, because the inline code may depend on the file. If you defer a script by hand with a filter, check the page for inline code that calls its functions.
Consent banners, critical menus and anything that paints above the fold should also stay put. A script that must run before the visitor sees the page is not a candidate for deferral. Defer the chat widget, the map, the social embeds and the analytics first, and leave the visible parts of the page alone.
How do you check that it worked?
Re-run PageSpeed Insights and click through the site the way a visitor would.
PageSpeed Insights is the check that matters, since its report named the problem in the first place.
WP Rocket's documentation notes that PageSpeed warns when JavaScript execution takes more than 2 seconds, and fails the check when it takes longer than 3.5 seconds.
Re-run the audit after each change, and compare the render-blocking list with the first run.
Then test by hand.
Open the home page, a post and a product or form page. Click the menu, submit the form, open the slider and check the checkout.
A script that breaks shows up as a dead button or a console error, and the fix is usually to exclude that one script from deferral or delay.
Check the page source too. A deferred script tag carries defer and a strategy marker, and a delayed script carries the placeholder type WP Rocket uses. If the tag looks unchanged, the setting did not apply to that handle, and the script may be printed by hand in a template.
Passing Core Web Vitals takes more than script timing, so check the full checklist.
What mistakes should you avoid?
The most common mistakes are deferring jQuery, delaying everything at once and never testing after the change.
First, never defer or delay jQuery and the scripts that depend on it. The errors appear in the console and in broken menus, and they are the first thing to check when a site breaks after an optimization change. Exclude jQuery and its dependents before you enable a site-wide switch.
Second, do not enable defer and delay together without testing each one alone. Delay already covers defer for most scripts, and running both can double-handle the same tags. Turn on one, test, then decide whether the second adds anything.
Third, do not trust a score without clicking the site. A perfect PageSpeed result means little if the menu, the form or the checkout stopped working. Test on a staging copy first, then on the live site, and keep a note of which scripts you excluded and why.
Fourth, do not defer scripts that a plugin prints by hand in a template. The core strategy only works on enqueued handles, so a hard-coded script tag ignores it. Find those tags in the theme files, and move them to proper enqueue calls before you expect the switch to work.
WP Rocket's Core Web Vitals setup covers the same ground, so use one guide at a time.
Frequently asked questions
What is the difference between defer, async and delay?
Defer downloads a script in the background and runs it after the page is parsed, in order. Async also downloads in the background but runs as soon as it arrives, with no order guarantee. Delay holds scripts until the visitor moves, scrolls or taps. Use defer for ordered scripts, async for independent ones.
Should I defer jQuery?
No. Deferring jQuery breaks the inline code and plugins that expect it to exist already. Menus, sliders and forms can stop working. Leave jQuery and its dependents alone, and defer the chat widget, map and analytics scripts first.
How do I find which scripts to defer?
Open PageSpeed Insights and run the audit. Its render-blocking requests list names the files, their sizes and the time each one costs. That list is the work list, and it also shows whether a later change helped.
Why did my slider disappear after I enabled Delay JavaScript?
It delays all JavaScript until user interaction, which can hide a slider, chat box or cookie banner until the visitor moves the mouse. Exclude the affected script from the delay instead of switching the whole feature off.
Can I defer JavaScript without a plugin?
Use the strategy argument in wp_enqueue_script with 'defer', or use wp_script_add_data on a handle a plugin already registered. Both need WordPress 6.3 or newer, and the handle must exist when your code runs.
Does deferring JavaScript improve page speed scores?
Yes, in most cases. A normal script tag stops the browser from building the page until the script runs. Deferring lets the page appear first, which moves First Contentful Paint and Largest Contentful Paint earlier.
Sources
- wp_enqueue_script() WordPress Developer Resources
- Registering scripts with async and defer attributes in WordPress 6.3 Make WordPress Core
- Load third-party JavaScript web.dev
- Delay JavaScript execution WP Rocket
- Reduce JavaScript execution time WP Rocket
- How to Defer Parsing of JavaScript in WordPress WP Rocket
Is your product missing, or something out of date?
Tell us what to add or correct. We read every note and reply when we need a source. A message does not buy a ranking.
Thanks — we review requests within 3 business days.
