wp_verify_nonce( string $nonce, string|int $action = -1 ): int|false
- Since
- 2.0.3
- Source
wp-includes/pluggable.php:2470
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
- Scaling
- Constant
- Instructions
- 13–69
- Plugin surface
- 2 hooks
- Called by
- 15
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 72.
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.
15 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
empty($nonce) | 13 | wp_get_current_user() |
empty($nonce) | 19 | wp_get_current_user(), apply_filters() |
!empty($nonce) && hash_equals() | 37 | wp_get_current_user(), wp_get_session_token(), wp_nonce_tick(), wp_hash(), hash_equals() |
!empty($nonce) && hash_equals() | 43 | wp_get_current_user(), apply_filters(), wp_get_session_token(), wp_nonce_tick(), wp_hash(), hash_equals() |
!empty($nonce) | 56 | wp_get_current_user(), wp_get_session_token(), wp_nonce_tick(), wp_hash(), hash_equals(), wp_hash(), hash_equals() |
!empty($nonce) | 62 | wp_get_current_user(), apply_filters(), wp_get_session_token(), wp_nonce_tick(), wp_hash(), hash_equals(), wp_hash(), hash_equals() |
!empty($nonce) && !hash_equals() | 63 | wp_get_current_user(), wp_get_session_token(), wp_nonce_tick(), wp_hash(), hash_equals(), wp_hash(), hash_equals(), do_action() |
!empty($nonce) && !hash_equals() | 69 | wp_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 72 | 13–69 | 4 | |
| 8.5 | 72 | 13–69 | 4 | |
| 8.4 | 72 | 13–69 | 4 | 7 fewer instructions than PHP 8.3 |
| 8.3 | 79 | 13–76 | 4 | |
| 8.2 | 79 | 13–76 | 4 | |
| 8.1 | 79 | 13–76 | 4 | |
| 7.4 | 79 | 13–76 | 4 |
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:
- apply_filters( nonce_user_logged_out )filterline 2483 (+13 into the body)
Filters whether the user who generated the nonce is logged out.
- do_action( wp_verify_nonce_failed )actionline 2515 (+45 into the body)
Fires when nonce verification fails.
Uses · 6
- wp_get_current_user()Retrieves the current user object.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_get_session_token()Retrieves the current session token from the logged_in cookie.
- wp_nonce_tick()Returns the time-dependent variable for nonce creation.
- wp_hash()Gets the hash of the given string.
- do_action()Calls the callback functions that have been added to an action hook.
Used by · 15
- Custom_Image_Header::step()Gets the current step.
- WP_Plugin_Install_List_Table::prepare_items()
- WP_Recovery_Mode::handle_exit_recovery_mode()Handles a request to exit Recovery Mode.
- _show_post_preview()Filters the latest content for preview from the post autosave.
- check_admin_referer()Ensures intent by verifying that a user was referred from another admin page with the correct security nonce.
- check_ajax_referer()Verifies the Ajax request to prevent processing requests external of the blog.
- redirect_canonical()Redirects incoming links to the proper URL based on the site url.
- request_filesystem_credentials()Displays a form to the user to request for their FTP/SSH details in order to connect to the filesystem.
- rest_cookie_check_errors()Checks for errors when using cookie-based authentication.
- signup_nonce_check()Processes the signup nonce created in signup_nonce_fields().
- wp_ajax_destroy_sessions()Handles destroying multiple open sessions for a user via AJAX.
- wp_ajax_heartbeat()Handles the Heartbeat API via AJAX.
Show all 15
- wp_autosave()Saves a post submitted with XHR.
- wp_edit_theme_plugin_file()Attempts to edit a file for a theme or plugin.
- wp_handle_comment_submission()Handles the submission of a comment, usually posted to wp-comments-post.php via a comment form.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 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.