WordPress Nonces

A nonce is a "number used once" to help protect URLs and forms from certain types of misuse, malicious or otherwise. WordPress nonces aren't numbers, but are a hash made up of numbers and letters. Nor are they used only once, but have a limited "lifetime" after which they expire. During that time period the same nonce will be generated for a given user in a given context. The nonce for that action will remain the same for that user until that nonce life cycle has completed.

WordPress's security tokens are called "nonces" despite the above noted differences from true nonces, because they serve much the same purpose as nonces do. They help protect against several types of attacks including CSRF, but do not protect against replay attacks because they aren't checked for one-time use.

For an example of how an nonce is used, an admin screen might generate a URL like this that trashes post number 123. You can see that the URL contains a nonce at the end:

Adding a nonce to a form

To add a nonce to a form, call wp_nonce_field() specifying a string representing the action. By default wp_nonce_field() generates two hidden fields, one whose value is the nonce and one whose value is the current URL (the referrer), and it echoes the result. For example, this call:

For maximum protection, ensure that the string representing the action is as specific as possible.

You can specify a different name for the nonce field, you can specify that you do not want a referrer field, and you can specify that you want the result to be returned and not echoed. For details of the syntax, see: wp_nonce_field()

Creating a nonce for use in some other way

To create a nonce for use in some other way, call wp_create_nonce() specifying a string representing the action. For example:

$nonce = wp_create_nonce( 'my-action_'.$post->ID );

This simply returns the nonce itself. For example: 295a686963

For maximum protection, ensure that the string representing the action is as specific as possible.

Verifying a nonce

You can verify a nonce that was passed in a URL, a form in an admin screen, an AJAX request, or in some other context.

Verifying a nonce passed from an admin screen

To verify a nonce that was passed in a URL or a form in an admin screen, call check_admin_referer() specifying the string representing the action. For example:

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

This call checks the nonce and the referrer, and if the check fails it takes the normal action (terminating script execution with a "403 Forbidden" response and an error message).

If you did not use the default field name (_wpnonce) when you created the nonce, specify the field name. For example:

check_admin_referer( 'delete-comment_'.$comment_id, 'my_nonce' );

Verifying a nonce passed in an AJAX request

To verify a nonce that was passed in an AJAX request, call check_ajax_referer() specifying the string representing the action. For example:

check_ajax_referer( 'process-comment' );

This call checks the nonce (but not the referrer), and if the check fails then by default it terminates script execution.

If you did not use one of the default field names (_wpnonce or _ajax_nonce) when you created the nonce, or if you want to take some other action instead of terminating execution, you can specify additional parameters. For details, see: check_ajax_referer()

Verifying a nonce passed in some other context

To verify a nonce passed in some other context, call wp_verify_nonce() specifying the nonce and the string representing the action. For example:

If the result is false, do not continue processing the request. Instead take some appropriate action. The usual action is to call wp_nonce_ays(), which sends a "403 Forbidden" response to the browser with the error message: "Are you sure you want to do this?".

Modifying the nonce system

You can modify the nonce system by adding various actions and filters.

Modifying the nonce lifetime

By default, a nonce has a lifetime of one day. After that, the nonce is no longer valid even if it matches the action string. To change the lifetime, add a nonce_life filter specifying the lifetime in seconds. For example, to change the lifetime to four hours:

Note that just as a WordPress nonce is not "a number used once", nonce lifetime isn't really nonce lifetime. WordPress uses a system with two ticks (half of the lifetime) and validates nonces from the current tick and the last tick. In default settings (24h lifetime) this means that the time information in the nonce is related to how many 12h periods of time have passed since Unix epoch. This means that a nonce made between midday and midnight will have a lifetime until midday the next day. The actual lifetime is thus variable between 12 and 24 hours. The example above will give you nonces that are valid for 2-4 hours.

Performing additional verification

To perform additional verification when check_admin_referrer() has found that the nonce and the referrer are valid, add a check_admin_referer action. For example:

Additional information

This section contains additional information about the nonce system in Wordpress that might occasionally be useful.

Nonce lifetime

The lifetime of a nonce is divided into two ticks. When a nonce is valid, the functions that validate nonces return the current tick number, 1 or 2. You could use this information, for example, to refresh nonces that are in their second tick so that they do not expire.

Nonce security

Nonces are generated using a key and salt that are unique to your site if you have installed WordPress correctly. NONCE_KEY and NONCE_SALT are defined in your wp-config.php file, and the file contains comments that provide more information.

Replacing the nonce system

Some of the functions that make up the nonce system are pluggable, so that you can replace them by supplying your own functions.

To change the way admin requests or AJAX requests are verified, you can replace check_admin_referrer() or check_ajax_referrer(), or both.

To replace the nonce system with some other nonce system, you can replace wp_create_nonce(), wp_verify_nonce() and wp_nonce_tick().