HomeGuides › Building and placing forms

Why contact forms break behind a page cache

WordPress forms usually carry a security token (a nonce) in the page. A caching plugin saves that page and serves it for hours or days, and once the token is older than WordPress allows, submissions are rejected. The fix is to fetch a fresh token when the visitor starts filling in the form, and to handle an expired one gracefully.

What goes wrong

A nonce in WordPress is valid for 12 to 24 hours. A page cached by LiteSpeed Cache, WP Super Cache, W3 Total Cache or a host's server cache can be much older than that. The visitor fills in the form, presses Send, and gets a vague error — or, in some plugins, a success message for a message that was thrown away.

How Mailbag handles it

  • A fresh token on first use. When a visitor first puts the cursor in the form, Mailbag's script asks the site for a new token and time stamp. That request is a POST, which caches don't store, so the answer is always current.
  • A friendly retry without JavaScript. If the token is too old — because scripts didn't run — the visitor sees “Please press Send again. The page had been open for a long time.”, with everything they typed still in the form. The second press works.
  • The time trap still works. Mailbag's anti-bot time check uses a signed time stamp that's refreshed together with the token, so a cached page doesn't make every visitor look like a bot.

Should you exclude the contact page from the cache?

With Mailbag you don't need to. Excluding pages is a common workaround for plugins that break, but it costs speed on one of your most important pages. If your cache has an option to exclude pages that contain forms, it's harmless, just unnecessary.

Testing it

Load your contact page, let the cache store it, and come back the next day — or simulate it by temporarily setting a very long cache lifetime. Send the form with and without JavaScript. Check that the entry appears under Mailbag → Entries both times. Because Mailbag saves every entry, a failure can't hide: a message either shows up there or the visitor was told it wasn't sent.

Mailbag Contact Form is a free contact form that saves every entry before it sends anything, and shows you what happened to each email.

See what it does