check_admin_referer( int|string $action = -1, string $query_arg = '_wpnonce' ): int|false
- Since
- 1.2.0, 2.5.0
- Source
wp-includes/pluggable.php:1372
Description
This function ensures the user intends to perform a given action, which helps protect against clickjacking style attacks. It verifies intent, not authorization, therefore it does not verify the user's capabilities. This should be performed with current_user_can() or similar.
If the nonce value is invalid, the function will exit with an "Are You Sure?" style message.
Compatibility
- WordPress
- since 2.5.0
- 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
$actionint|stringoptional- The nonce action.Default:
-1 $query_argstringoptional- Key to check for nonce in
$_REQUEST. Default '_wpnonce'.Default:'_wpnonce'
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 check_admin_referer() 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
- 28–53
- Plugin surface
- 1 hook
- Called by
- 12
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 54.
Third-party callbacks on 'check_admin_referer' run inside this call, and their cost is not bounded by anything here.
12 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_action()called directly
Further down the call graph this can also reach option, cache, serialize, query and transient. 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 check_admin_referer() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$action !== -1 && !isset($query_arg) | 28–32 | admin_url(), strtolower(), wp_get_referer(), strtolower(), do_action() |
$action !== -1 && !isset($query_arg) | 35–37 | admin_url(), strtolower(), wp_get_referer(), strtolower(), do_action(), wp_nonce_ays(), exit() |
$action !== -1 && isset($query_arg) | 36–40 | admin_url(), strtolower(), wp_get_referer(), strtolower(), wp_verify_nonce(), do_action() |
$action === -1 && !isset($query_arg) | 36–40 | __(), _doing_it_wrong(), admin_url(), strtolower(), wp_get_referer(), strtolower(), do_action() |
$action !== -1 && isset($query_arg) | 43–45 | admin_url(), strtolower(), wp_get_referer(), strtolower(), wp_verify_nonce(), do_action(), wp_nonce_ays(), exit() |
$action === -1 && !isset($query_arg) | 43–45 | __(), _doing_it_wrong(), admin_url(), strtolower(), wp_get_referer(), strtolower(), do_action(), wp_nonce_ays(), exit() |
$action === -1 && isset($query_arg) | 44–48 | __(), _doing_it_wrong(), admin_url(), strtolower(), wp_get_referer(), strtolower(), wp_verify_nonce(), do_action() |
$action === -1 && isset($query_arg) | 51–53 | __(), _doing_it_wrong(), admin_url(), strtolower(), wp_get_referer(), strtolower(), wp_verify_nonce(), do_action(), wp_nonce_ays(), exit() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 53 | 28–52 | 5 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 54 | 28–53 | 5 | |
| 8.4 | 54 | 28–53 | 5 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 56 | 28–54 | 5 | |
| 8.2 | 56 | 28–54 | 5 | |
| 8.1 | 56 | 28–54 | 5 | |
| 7.4 | 56 | 28–54 | 5 |
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 · 1
One hook fires while check_admin_referer() runs, in this order:
- do_action( check_admin_referer )actionline 1390 (+18 into the body)
Fires once the admin request has been validated or not.
Uses · 8
- _doing_it_wrong()Marks something as being incorrectly called.
- __()Retrieves the translation of $text.
- admin_url()Retrieves the URL to the admin area for the current site.
- wp_get_referer()Retrieves referer from '_wp_http_referer' or HTTP referer.
- wp_verify_nonce()Verifies that a correct security nonce was used with time limit.
- do_action()Calls the callback functions that have been added to an action hook.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- wp_nonce_ays()Displays "Are You Sure" message to confirm the action being taken.
Used by · 12
- Custom_Background::handle_upload()Handles an Image upload for the background image.
- Custom_Background::take_action()Executes custom background modification.
- Custom_Image_Header::step_2()Displays second step of custom header image page.
- Custom_Image_Header::step_3()Displays third step of custom header image page.
- Custom_Image_Header::take_action()Executes custom header modification.
- WP_Privacy_Requests_Table::process_bulk_action()Process bulk actions.
- _wp_personal_data_handle_actions()Handle list table actions.
- media_upload_form_handler()Handles form submissions for the legacy media uploader.
- set_screen_options()Saves option for number of rows when listing posts, pages, comments, etc.
- use_block_editor_for_post()Returns whether the post can be edited in the block editor.
- wp_dashboard_setup()Registers dashboard widgets.
- wp_media_upload_handler()Handles the process of uploading media.
Source code
function check_admin_referer( $action = -1, $query_arg = '_wpnonce' ) { if ( -1 === $action ) { _doing_it_wrong( __FUNCTION__, __( 'You should specify an action to be verified by using the first parameter.' ), '3.2.0' ); } $adminurl = strtolower( admin_url() ); $referer = strtolower( wp_get_referer() ); $result = isset( $_REQUEST[ $query_arg ] ) ? wp_verify_nonce( $_REQUEST[ $query_arg ], $action ) : false; /** * Fires once the admin request has been validated or not. * * @since 1.5.1 * * @param string $action The nonce action. * @param false|int $result False if the nonce is invalid, 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. */ do_action( 'check_admin_referer', $action, $result ); if ( ! $result && ! ( -1 === $action && str_starts_with( $referer, $adminurl ) ) ) { wp_nonce_ays( $action ); die(); } return $result; }Changelog
Introduced in 1.2.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$query_arg parameter was added.from the docblockAbout 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.