Caching and login pages
Caching makes a site fast by saving a finished copy of a page and handing it to the next visitor. That’s a problem for a form with a one-time security token, because the saved copy holds an old one. This topic explains what Nahnu Login Shield already does about it, what can still go wrong, and what to exclude from caching and optimization.
What the plugin does for you#
Every form the plugin protects carries a hidden security field called a form nonce. It proves that the submission came from a real page that your site served, and the plugin checks it before it reads anything else the form sent. The nonce lives for about one day.
To keep cached pages working, the plugin does two things:
- On page load, a very small script asks your site for a fresh nonce (one request to
admin-ajax.php) and fills it into every protected form on the page. A cached page that is older than a day therefore keeps working, as long as JavaScript runs. - The same script sets the hidden JavaScript check field, which proves a real browser loaded the page.
This covers protected forms on cached pages, such as a comment form on a cached blog post.
What still needs your attention#
1. Don’t cache the login, registration and My Account pages
Exclude these from full-page caching:
/wp-login.php, or your custom login address if you use one (General tab → Custom login URL)- The WooCommerce My Account page and checkout (WooCommerce requires this anyway)
- Any page with a login, registration or password-reset form
- Any page that uses the Bricks or SureForms forms you protect, if the page also shows different content to logged-in users
Most caching plugins and hosts skip wp-login.php already. Check the exclusion list in your caching plugin, and for a custom login address, add it yourself.
2. Let the scripts run
Optimization plugins can delay, defer, combine or minify JavaScript. If they delay the plugin’s scripts, the hidden JavaScript check never changes to “on” and the nonce is never refreshed. Visitors then see:
- “JavaScript verification failed. Please enable JavaScript and try again.” (Custom forms that use the developer API show “JavaScript is required. Please enable JavaScript and try again.”)
- “Security check expired. Please reload the page and try again.”
- A CAPTCHA widget that never appears.
Fix: in your optimization plugin (WP Rocket, LiteSpeed Cache, Autoptimize, Perfmatters, SG Optimizer, W3 Total Cache and similar), exclude Nahnu Login Shield’s scripts from Delay JavaScript, Defer, Combine and Minify. Exclude by path:
/wp-content/plugins/nahnu-login-shield/ At minimum, exclude js-marker.js and widget.js, and your CAPTCHA provider’s script (Google reCAPTCHA, hCaptcha, Cloudflare Turnstile, Friendly Captcha).
3. Don’t block or cache admin-ajax.php
The nonce refresh uses /wp-admin/admin-ajax.php. If a security plugin, firewall rule, CDN rule or host setting blocks admin-ajax.php for visitors who aren’t logged in, or caches its response, the refresh fails. The form still works while the cached nonce is fresh (less than a day old), then breaks.
Check that:
admin-ajax.phpis reachable for logged-out visitors.- Your CDN does not cache it. The plugin sends no-cache headers with the response, but “cache everything” rules can override them.
4. Check CDN “Cache Everything” rules
A rule such as Cloudflare’s Cache Everything can cache HTML pages that contain login forms, or serve a logged-in version to a logged-out visitor. Add a bypass rule for:
- The login address
- The My Account and checkout pages
- Any page with a protected form that shouldn’t be cached
If you leave a protected form on a cached page on purpose (a comment form on cached posts, for example), keep the scripts running (points 2 and 3) and consider lengthening the nonce life (below).
5. Long cache times and the nonce life
If a cached copy of a page can be older than a day and a visitor has JavaScript off, their form will fail. You can lengthen the nonce life with a small code snippet (in your child theme or a code snippets plugin):
add_filter( 'nahnls_form_nonce_life', function () {
return 2 * DAY_IN_SECONDS;
} ); This helps only the old-page case. It does not replace the exclusions above.
Other timeouts that look like caching problems#
These aren’t caching, but they show similar symptoms:
- Leaving a login page open. The plugin’s own verification token (for the OOPSpam widget) is valid for 20 minutes. A visitor who leaves the page open longer and then submits has to complete the check again.
- CAPTCHA providers. Google reCAPTCHA, hCaptcha and Turnstile tokens also expire, usually within a few minutes. A visitor who solves the CAPTCHA and then waits a long time before submitting must solve it again.
- Wrong server time. A server clock that is far off can make nonces and tokens appear expired. Ask your host.
How to test#
- Clear your page cache.
- Open the page in a private window (logged out). Look at the page source: the form should contain a hidden
nahnls_form_noncefield. - In your browser’s developer tools (Network tab), reload and look for the request
admin-ajax.php?action=nahnls_form_nonce. It should return a successful response. - Submit the form. It should go through.
- For a cached page, wait for the cache to hold a copy, reload, and repeat. Then, with the scripts excluded from delay and combine, test again.
- If you see “Security check expired”, the nonce refresh didn’t run. Check the three points above (scripts, admin-ajax.php, cache rules).
Quick fixes for common symptoms#
| Symptom | Likely cause | Fix |
|---|---|---|
| “JavaScript verification failed” or “JavaScript is required” even though JavaScript is on | Script delayed, deferred or combined by an optimization plugin | Exclude the plugin’s scripts from optimization |
| “Security check expired. Please reload the page” | Cached page older than the nonce life, or admin-ajax.php blocked | Exclude the page from caching, unblock admin-ajax.php, or lengthen the nonce life |
| CAPTCHA widget doesn’t appear | Provider script blocked or delayed | Exclude it from optimization, and from any content security policy that blocks it |
| Works for admins but not for visitors | Logged-in users skip the cache | Test in a private window |
| Works after a hard refresh, fails on the first visit | Page cache serving an old copy, scripts delayed | Same as above |
Was this helpful?
Thanks for your feedback!
✓ Feedback received. Thank you!