Skip to content
FunnelGuard

Fix guide by FunnelsGuard

Cached page serves stale nonce: fix WordPress form errors

CNV-381High impactDetectedBy FunnelGuardPublished Updated
On this page

What this issue is

WordPress protects forms, logins and AJAX requests with a nonce. A nonce is a short code tied to a time window and, for logged-out visitors, mostly to the site rather than to one person. The page prints it into the HTML, usually as a hidden field such as _wpnonce, or into a JavaScript variable. When the visitor submits, WordPress checks the code. If it is too old, the request fails.

The page looks fine. It loads fast and the form shows. The failure only appears when someone submits. They see a message like "Are you sure you want to do this?", "The link you followed has expired", a 403 error, or a spinner that never ends. Most people do not try again.

Where a stale nonce breaks the path
  1. Cache saves pageHTML stored with nonce inside
  2. Visitor loads pageGets the old cached copy
  3. Visitor submits formOld nonce sent to the server
  4. Lead or orderNever created
  5. Conversion trackingThank-you page never reached

The visitor does nothing wrong. The token in the cached page has already expired.

Submit success by cache ageExample numbers
Submit success by cache age (example)Cache 2 hours old: 98 % of submits accepted; Cache 12 hours old: 90 % of submits accepted; Cache 30 hours old: 5 % of submits accepted0387598905Cache 2 hours oldCache 12 hours oldCache 30 hours old% of submits accepted

Example: illustrative numbers showing how acceptance drops once the cached copy is older than the nonce lifetime.

How to check if your site has it

Test as a real visitor
  1. Open the page logged out

    Use a private window with no WordPress login cookie. Logged-in users often skip the cache, so testing while logged in hides the problem.

  2. Look for a cache header

    In browser DevTools, Network tab, click the page request. Look for headers such as x-cache: HIT, cf-cache-status: HIT, x-litespeed-cache: hit or x-wp-cache. A hit means you are seeing a stored copy.

  3. Find the nonce in the source

    View page source and search for _wpnonce, nonce or your form plugin's token field. Note the value.

  4. Compare with a fresh load

    Purge the cache, reload, and compare the value. If it is identical across many reloads for hours, it is baked into the cached page.

  5. Submit after the cache has aged

    Submit the form from a page copy older than 24 hours. If it fails with an expiry or 403 message while a fresh copy works, a stale nonce is the cause.

How to fix it

You have two routes. The simple one is to stop caching the page that holds the form. The better one for busy pages is to keep caching and have the token fetched fresh after the page loads. Start with the simple one.

Cache behaviour

Before

  • Form page cached for days
  • Nonce frozen in the HTML
  • Submits fail after the token expires

After

  • Form page excluded, or cache lifetime under 12 hours
  • Token is fresh when the page loads
  • Submits are accepted
Fix by setup
  1. Exclude the form page

    In your caching plugin (WP Rocket: Advanced Rules > Never Cache URL(s); W3 Total Cache: Page Cache > Never cache the following pages; LiteSpeed Cache: Cache > Excludes > Do Not Cache URIs), add the URL of the page with the form.

  2. Shorten cache lifetime

    If you must cache it, set the cache lifespan under 12 hours (WP Rocket: Cache > Cache Lifespan; LiteSpeed: Cache > TTL). Preloading helps keep copies fresh.

  3. Check form plugin settings

    Some form plugins offer a setting to load tokens by AJAX or to disable nonce on public forms. Look in the plugin's settings or docs, and keep spam protection on.

  4. Do not cache checkout and account pages

    For WooCommerce, cart, checkout and my-account pages must be excluded. Most cache plugins do this by default, so check it is still on.

  5. Purge everything

    Clear the plugin cache, then any host or CDN cache, so old copies are gone.

Uncached endpoint that returns a fresh nonce (add to a small plugin or child theme functions.php)php
add_action('wp_ajax_nopriv_fresh_nonce', 'fg_fresh_nonce');
add_action('wp_ajax_fresh_nonce', 'fg_fresh_nonce');
function fg_fresh_nonce() {
    nocache_headers();
    wp_send_json_success(array('nonce' => wp_create_nonce('my_form_action')));
}
Fill the hidden field with a fresh nonce on page loadjavascript
document.addEventListener('DOMContentLoaded', function () {
  var field = document.querySelector('input[name="_wpnonce"]');
  if (!field) return;
  fetch('/wp-admin/admin-ajax.php?action=fresh_nonce', { credentials: 'same-origin', cache: 'no-store' })
    .then(function (r) { return r.json(); })
    .then(function (j) { if (j.success) field.value = j.data.nonce; });
});

How to confirm it's fixed

Confirmation checks
  • Purge all caches (plugin, host, CDN).
  • In a private window, load the form page and submit it. It succeeds.
  • The response headers show no long-lived cache hit for the form page, or the nonce value changes between loads.
  • Wait until the old cache lifetime would have passed, then submit again. It still succeeds.
  • The test lead appears in your inbox and CRM, and the thank-you page loads.
  • Your conversion tag fires on the thank-you page.

How to prevent it happening again

Habits that stop it coming back
  • Keep a list of pages with forms, checkouts and logins, and keep them in your cache exclusion rules.
  • Keep cache lifetime under 12 hours wherever a nonce is printed.
  • Re-check exclusions after changing cache plugins, hosts, CDN or themes, because settings often reset.
  • Test the forms logged out after every plugin update and cache purge.
  • Run a scheduled test submit so a failure is caught within a day, not after weeks.

How FunnelGuard handles this

FunnelGuard detects this today. A robot visitor loads your page as a logged-out visitor, fills in and submits the form on a schedule, and checks the result. When the submit fails, as it does with a stale nonce, the step is marked failed. The failure is re-checked from a second region before one alert goes out by email, Slack, Teams, WhatsApp, webhook or PagerDuty, with a screenshot and a likely cause.

A cached token fails on submit

The page loads normally, then the submit is rejected.

Questions people ask

Why does my WordPress form say "Are you sure you want to do this?"

That message usually means a nonce check failed. A common cause is a cached page that still holds an expired nonce. Exclude the form page from caching or shorten the cache lifetime, then purge the cache.

How long does a WordPress nonce last?

By default a WordPress nonce is valid for up to 24 hours, in two 12-hour windows. If your cached page is older than that, the token inside is no longer accepted. Keep cache lifetime under 12 hours on pages that print a nonce.

Why does my form work when I'm logged in but fail for visitors?

Many caching setups skip the cache for logged-in users, so you get a fresh page and a fresh token. Visitors get the stored copy with an old token. Test in a private window while logged out.

Should I exclude my contact page from the cache?

If the page prints a nonce and a failing submit costs you leads, yes. It is the simplest fix. The speed cost for one page is small compared with losing submissions.

Does WooCommerce checkout break with page caching?

It can if the cart, checkout or my-account pages are cached. Most cache plugins exclude them by default, but check that the rule is still active after plugin or host changes.

How do I fix a stale nonce without turning off caching?

Keep the page cached and load the nonce fresh after the page loads, using a small uncached AJAX endpoint that fills the hidden field. Make sure that endpoint is never cached.

How can I find out my form is failing before customers tell me?

Run a scheduled test that submits the form as a logged-out visitor. FunnelGuard does this and alerts you when the submit fails, with a screenshot and a likely cause.

Does FunnelGuard catch this?

Detected

FunnelGuard checks for this today.

How: submit fails.

Stop finding out from your customers

FunnelGuard runs your real forms and checkouts on a schedule, checks the tracking and the leads behind them, and alerts you once, with a screenshot. Try any paid plan free for 3 days.