Plugins Plugin hub Backup SEO Caching Security Forms WooCommerce Images Best backup plugins
Hosting Hosting directory Shared hosting Managed WordPress Cloud hosting VPS hosting By reader location Hosting finder
Themes & builders Compare Rank Math vs Yoast
Tools All tools Hosting finder Plugin stack builder Cost calculator Salt generator Theme detector Who is hosting this? Speed test Headline analyzer robots.txt generator Redirect generator Uptime calculator Blog name generator
Blog Best backup plugins Deals
Guides All guides Speed up WordPress Choose hosting Back up WordPress
How we test

Developer · Guide

How to Use Nonces in WordPress Forms and AJAX

How to Use Nonces in WordPress Forms and AJAX

Short answer

To use nonces in WordPress forms and AJAX, create a token with wp_create_nonce or wp_nonce_field using an action string that names the exact job, send it with the form or AJAX call, then check it on the server with check_admin_referer for forms, check_ajax_referer for AJAX, or wp_verify_nonce for other cases. Always add a current_user_can check, since a nonce proves the request came from your site, not that the user may run it.

Key takeaways

  • A WordPress nonce is a hash token tied to a user, an action, and a time window, not a true one-use number.
  • Nonces stop cross-site request forgery but do not stop replay attacks or prove permission.
  • A nonce lasts one day by default, with a real window of 12 to 24 hours across two 12-hour ticks.
  • Forms use wp_nonce_field, links use wp_nonce_url, and AJAX checks use check_ajax_referer.
  • Always pair a nonce check with current_user_can, since a nonce never proves what a user may do.

What is a WordPress nonce?

A WordPress nonce is a short hash token tied to one user, one action, and one time window that proves a request came from your own site.

A nonce is a “number used once” to help protect URLs and forms from certain types of misuse, malicious or otherwise. Developer.WordPress.org gives that definition in the Common APIs handbook (checked November 2026).

WordPress nonces are not true one-use numbers. Developer.WordPress.org states that they are a hash made up of numbers and letters, that they are not used only once, and that they have a limited lifetime after which they expire (checked November 2026).

During that lifetime, the same nonce is made again for the same user in the same context. That is normal, and it is why a nonce must always sit beside a permission check, never in place of one.

A nonce is built from the current user ID, the user session token, a time value, and the action name. Developer.WordPress.org states that wp_create_nonce() takes the user ID and session token values, adds two more values, and hashes them at least twice (checked November 2026).

What attacks do nonces stop, and what do they not stop?

Nonces stop cross-site request forgery, where another site tricks a signed-in browser into running an unwanted action on your site.

A cross-site request forgery attack occurs when a harmful site tricks an authenticated user's browser into doing an unwanted action on a trusted site. OWASP gives that definition in its prevention cheat sheet (checked November 2026).

The attack works because the browser sends login cookies with every request, even one started by another site. OWASP states that browser requests send all cookies, including session cookies, by default (checked November 2026).

MDN Web Docs states that a CSRF attack is possible when a site changes state with HTTP requests, checks only cookies to confirm the user, and uses only request values an attacker can guess (checked November 2026).

A nonce breaks that pattern. The attacker cannot guess the token, because it is a hash tied to the victim's session and to the exact action name.

Nonces do not stop replay attacks. Developer.WordPress.org states that nonces help protect against several types of attacks including CSRF, but do not protect against replay attacks because they are not checked for one-time use (checked November 2026).

Nonces also do not prove who the user is or what the user may do. Developer.WordPress.org states that nonces should never be relied on for authentication, authorization, or access control, and that functions must be guarded with current_user_can() (checked November 2026).

How long does a WordPress nonce last?

A nonce lasts one day by default, but the real window is between 12 and 24 hours, based on when it was made.

By default, a nonce has a lifetime of one day. Developer.WordPress.org states that after that time the nonce is no longer valid, even if it matches the action string (checked November 2026).

WordPress splits the day into two 12-hour ticks and accepts nonces from the current tick and the last tick. Developer.WordPress.org states that in default settings the real lifetime is thus variable between 12 and 24 hours (checked November 2026).

A nonce made just after a tick starts can live close to 24 hours. A nonce made just before a tick ends can live just over 12 hours, since only the next tick remains.

The check returns 1, 2, or false. Developer.WordPress.org states that a valid nonce made 0 to 12 hours ago returns 1, one made 12 to 24 hours ago returns 2, and an invalid nonce returns false (checked November 2026).

Change the lifetime with the nonce_life filter, which sets the life in seconds. Developer.WordPress.org gives this example for a 4-hour life (checked November 2026):

add_filter( 'nonce_life', function () { return 4 * HOUR_IN_SECONDS; } );

The default life is 86,400 seconds, which is one day. Developer.WordPress.org states that default of 86,400 seconds in its nonce guide (checked November 2026).

How do you add a nonce to a form?

Add a nonce to a form with wp_nonce_field(), which prints a hidden field holding the token plus a hidden referrer field.

Call wp_nonce_field() with a string that names the action. Developer.WordPress.org states that the call makes two hidden fields by default, one whose value is the nonce and one whose value is the current URL (checked November 2026).

For a form that deletes one comment, the call looks like this:

wp_nonce_field( 'delete-comment_' . $comment_id );

That call prints a hidden field named _wpnonce holding a token such as 796c7766b1, plus a hidden field named _wp_http_referer holding the current URL. Developer.WordPress.org shows both fields in its form example (checked November 2026).

Make the action string as exact as you can. Developer.WordPress.org states that for top protection, the string for the action should be as exact as possible, so add the item ID to it (checked November 2026).

Use one action string per form. A generic string such as “save” would let a nonce made for one form pass on another form, which defeats the point of naming the action.

A guide on how to make AJAX requests in WordPress shows the same token pattern used for requests that run without a page reload.

Add a nonce to a URL with wp_nonce_url(), which appends the token as a query value named _wpnonce by default.

An admin screen might make a URL that trashes post 123. Developer.WordPress.org gives that example and notes that WordPress checks the login cookie first, then would trash the post if the user may delete it (checked November 2026).

An attacker could hide that bare URL in an image tag on another page, and the browser would call it with the victim's cookies attached. Developer.WordPress.org gives that image-tag example as the reason a nonce is needed (checked November 2026).

With a nonce, the URL carries a token the attacker cannot guess. Developer.WordPress.org states that when anyone tries to trash post 123 without the correct nonce, WordPress sends a “403 Forbidden” response (checked November 2026).

Build the safe URL like this:

$complete_url = wp_nonce_url( $bare_url, 'trash-post_' . $post->ID );

You can use a different field name by passing it as a third value. Developer.WordPress.org states that wp_nonce_url() adds a field named _wpnonce by default, and that you can name a different field in the call (checked November 2026).

Use nonces only on URLs that change state, such as trash, delete, or approve. A plain link that only shows a page needs no token.

How do you use a nonce with AJAX?

Pass a nonce to the script with wp_localize_script(), send it with each AJAX call, and check it with check_ajax_referer() in the handler.

All AJAX requests in WordPress go through wp-admin/admin-ajax.php. Developer.WordPress.org states that all AJAX requests need to be sent through that file, and that you should never send requests straight to your plugin pages (checked November 2026).

Make the nonce when you load the script, with a clear action name:

$title_nonce = wp_create_nonce( 'title_example' );

Developer.WordPress.org gives this exact example and notes that the string can be any value, though one tied to the use is best (checked November 2026).

Hand the nonce and the AJAX address to the script with wp_localize_script(). The script then sends the nonce as _ajax_nonce or _wpnonce with each call.

On the server, the first step in the handler is the check:

check_ajax_referer( 'title_example' );

Developer.WordPress.org states that the first thing the handler should do is check the nonce sent by the script with check_ajax_referer(), using the same value that was localized (checked November 2026).

If the nonce fails, the function stops the request by default. Developer.WordPress.org states that the call checks the nonce but not the referrer, and that a failed check ends script run by default (checked November 2026).

How do you use a nonce with the REST API?

Use a nonce with the action wp_rest for cookie-based REST calls from inside WordPress, sent as the X-WP-Nonce header or the _wpnonce value.

Cookie auth is the standard method inside WordPress. Developer.WordPress.org states that the API uses nonces with the action set to wp_rest to avoid CSRF issues (checked November 2026).

Make the nonce in PHP and pass it to the script:

wp_localize_script( 'wp-api', 'wpApiSettings', array(
    'root'  => esc_url_raw( rest_url() ),
    'nonce' => wp_create_nonce( 'wp_rest' ),
) );

Developer.WordPress.org gives this example of how the built-in JavaScript client makes the nonce (checked November 2026).

Send it as a header on each call:

xhr.setRequestHeader( 'X-WP-Nonce', wpApiSettings.nonce );

Developer.WordPress.org states that the nonce can be passed through the _wpnonce data value or through the X-WP-Nonce header, and that with no nonce the API sets the current user to 0 (checked November 2026).

A guide on how to create a custom REST API endpoint in WordPress shows how custom routes also rely on this same cookie-plus-nonce check for signed-in users.

How do you check a nonce on the server?

Check a nonce with the function that matches where the request came from, then check the user permission before you act.

Where the request comes fromCheck functionWhat it checks
Admin form or action URLcheck_admin_referer()Nonce and referrer
AJAX callcheck_ajax_referer()Nonce only
Any other contextwp_verify_nonce()Nonce only, you decide the result

For an admin form, call check_admin_referer() with the action string. Developer.WordPress.org states that the call checks the nonce and the referrer, and that a failed check ends the script with a “403 Forbidden” response (checked November 2026).

For full control, use wp_verify_nonce() with the sent value and the action string:

wp_verify_nonce( $_POST['my_nonce'], 'process-comment' . $comment_id );

Developer.WordPress.org states that if the result is false, you must not go on with the request, and that the usual step is to call wp_nonce_ays(), which sends a “403 Forbidden” response (checked November 2026).

Always pair the nonce check with a permission check. A valid nonce proves the request came from your site, not that the user may run the action, so call current_user_can() with the right capability before you change anything.

A guide on how to add custom user roles in WordPress explains how roles and capabilities map to the checks your handler should run.

What mistakes should you avoid?

Avoid a generic action string, a missing permission check, and a nonce check that runs after the action has already happened.

Never use the same action string for every form. A nonce for “save” on a settings page would then pass on a delete form too, so name each action after the exact job and item.

Never skip current_user_can(). Developer.WordPress.org states that nonces can be compromised and that you should always assume they can be, so guard functions with a permission check (checked November 2026).

Never check the nonce after you have already changed data. The check must be the first step in the handler, before any delete, update, or send.

Do not treat a nonce as a login. Guests share user ID 0 by default, so a nonce for a guest action does not tell you which visitor sent it. Developer.WordPress.org states that core makes the same nonce for guests, since they share user ID 0 (checked November 2026).

Do not put a nonce in a cached page and expect it to last. A form left open overnight can fail, because the token may pass its 12 to 24 hour window before the user submits it.

Keep WordPress and PHP current. WordPress.org states that WordPress recommends PHP 8.3 or greater, with HTTPS support (checked November 2026).

A guide on how to run a WordPress security audit covers how to review forms and handlers for these gaps across a whole site.

How do you check that it worked?

Check it by submitting the form once with the right token, once with a changed token, and once with no token at all.

Submit the form as normal first. The action should run, and the page should show the result you expect, such as the comment deleted or the setting saved.

Change one character in the hidden nonce value with the browser developer tools and submit again. The request should fail with a 403 error or the “Are you sure you want to do this?” message, and the action must not run.

Remove the nonce field and submit a third time. That request must also fail, which proves the handler checks the token instead of trusting the form alone.

A guide on how to check the most common WordPress vulnerabilities lists the forgery flaws to look for when a form still accepts a request with no valid token.

Frequently asked questions

Can a WordPress nonce be used only once?

No. WordPress nonces can be used more than once while they are still valid. The name comes from security terms, but WordPress uses them as time-limited tokens tied to a user, an action, and a time window.

How long does a WordPress nonce last?

By default a nonce lasts one day, but the real window is 12 to 24 hours because WordPress checks the current 12-hour tick and the last one. A form left open overnight can fail if the token passes its window before submit.

Does a valid nonce mean the user may run the action?

No. A nonce proves the request came from your own site, not that the user may run the action. Always call current_user_can with the right capability after the nonce check, and stop the request if either check fails.

How do I send a nonce with a REST API call?

Use wp_create_nonce with the wp_rest action and send it as the X-WP-Nonce header or the _wpnonce value. With no nonce, the API treats the call as unauthenticated and sets the current user to 0.

How can I check my nonce check works?

Change one character in the hidden nonce value and submit again. The request should fail with a 403 error and the action must not run. Then try with no nonce field at all, which must also fail.

Can I change how long a nonce lasts?

Yes, by adding a nonce_life filter that returns the life in seconds. For a 4-hour life, return 4 times HOUR_IN_SECONDS. Shorter lives suit sensitive forms, while longer lives suit forms users keep open for a while.

Sources

  1. Nonces WordPress Developer Resources
  2. Understand and use WordPress nonces properly WordPress Developer Resources
  3. Authentication (REST API Handbook) WordPress Developer Resources
  4. Server Side PHP and Enqueuing WordPress Developer Resources
  5. Cross-Site Request Forgery Prevention Cheat Sheet OWASP
  6. Cross-site request forgery (CSRF) MDN Web Docs
  7. Requirements WordPress.org

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.

About the editors

Written and checked byWPLegible Editorial Team Independent WordPress reviews and guides

Researched from the primary sources listed above and checked against our editorial rules before publishing.