wppaste
WordPress

wp_verify_nonce( string $nonce, string|int $action = -1 ): int|false

Since
2.0.3
Source
wp-includes/pluggable.php:2474
Verifies that a correct security nonce was used with time limit.

Description

A nonce is valid for between 12 and 24 hours (by default).

Compatibility

WordPress
since 2.0.3
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$noncestring
Nonce value that was used for verification, usually via a form field.
$actionstring|intoptional
Should give context to what is taking place and be the same when nonce was created.Default: -1

Return value

int|false
1 if the nonce is valid and generated between 0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago.
False if the nonce is invalid.

Performance profile

How much work a call to wp_verify_nonce() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
13–69

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 72.

Plugin surface
2 hooks

Third-party callbacks on 'nonce_user_logged_out', 'wp_verify_nonce_failed' run inside this call, and their cost is not bounded by anything here.

Called by
15

15 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly

Further down the call graph this can also reach option, cache, serialize, transient and query. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 8 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_verify_nonce() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
empty($nonce)13wp_get_current_user()
empty($nonce)19wp_get_current_user(), apply_filters()
!empty($nonce) && hash_equals()37wp_get_current_user(), wp_get_session_token(), wp_nonce_tick(), wp_hash(), hash_equals()
!empty($nonce) && hash_equals()43wp_get_current_user(), apply_filters(), wp_get_session_token(), wp_nonce_tick(), wp_hash(), hash_equals()
!empty($nonce)56wp_get_current_user(), wp_get_session_token(), wp_nonce_tick(), wp_hash(), hash_equals(), wp_hash(), hash_equals()
!empty($nonce)62wp_get_current_user(), apply_filters(), wp_get_session_token(), wp_nonce_tick(), wp_hash(), hash_equals(), wp_hash(), hash_equals()
!empty($nonce) && !hash_equals()63wp_get_current_user(), wp_get_session_token(), wp_nonce_tick(), wp_hash(), hash_equals(), wp_hash(), hash_equals(), do_action()
!empty($nonce) && !hash_equals()69wp_get_current_user(), apply_filters(), wp_get_session_token(), wp_nonce_tick(), wp_hash(), hash_equals(), wp_hash(), hash_equals(), do_action()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev7213–694
8.57213–694
8.47213–6947 fewer instructions than PHP 8.3
8.37913–764
8.27913–764
8.17913–764
7.47913–764

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while wp_verify_nonce() runs, in this order:

  1. apply_filters( nonce_user_logged_out )filterline 2487 (+13 into the body)

    Filters whether the user who generated the nonce is logged out.

  2. do_action( wp_verify_nonce_failed )actionline 2519 (+45 into the body)

    Fires when nonce verification fails.

Uses · 6

Used by · 15

Show all 15

Source code

	function wp_verify_nonce( $nonce, $action = -1 ) {		$nonce = (string) $nonce;		$user  = wp_get_current_user();		$uid   = (int) $user->ID;		if ( ! $uid ) {			/**			 * Filters whether the user who generated the nonce is logged out.			 *			 * @since 3.5.0			 *			 * @param int        $uid    ID of the nonce-owning user.			 * @param string|int $action The nonce action, or -1 if none was provided.			 */			$uid = apply_filters( 'nonce_user_logged_out', $uid, $action );		} 		if ( empty( $nonce ) ) {			return false;		} 		$token = wp_get_session_token();		$i     = wp_nonce_tick( $action ); 		// Nonce generated 0-12 hours ago.		$expected = substr( wp_hash( $i . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 );		if ( hash_equals( $expected, $nonce ) ) {			return 1;		} 		// Nonce generated 12-24 hours ago.		$expected = substr( wp_hash( ( $i - 1 ) . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 );		if ( hash_equals( $expected, $nonce ) ) {			return 2;		} 		/**		 * Fires when nonce verification fails.		 *		 * @since 4.4.0		 *		 * @param string     $nonce  The invalid nonce.		 * @param string|int $action The nonce action.		 * @param WP_User    $user   The current user object.		 * @param string     $token  The user's session token.		 */		do_action( 'wp_verify_nonce_failed', $nonce, $action, $user, $token ); 		// Invalid nonce.		return false;	}

Changelog

Introduced in 2.0.3. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/pluggable.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.