Codex tools: Log in
Contents |
Tests either if the current request carries a valid nonce, or if the current request was referred from an administration screen; depending on whether the $action argument is given (which is prefered), or not, respectively. On failure, the function dies after calling the wp_nonce_ays() function.
Used to avoid security exploits.
The now improper name of the function is kept for backward compatibility and has origin in previous WordPress versions where the function only checked the referer. For details, see the Notes section below.
<?php check_admin_referer(); ?>
<?php check_admin_referer( $action, $query_arg ); ?>
To return boolean true, in the case of the obsolete usage, the current request must be referred from an administration screen; in the case of the prefered usage, the nonce must be sent and valid. Otherwise the function dies with an appropriate message ("Are you sure you want to do this?" by default).
Obsolete usage here (script dies if the admin referer is not validated).
<?php check_admin_referer(); ?>
Here is an example of how you might use this in a plugin's option page. You add a nonce to a form using the wp_nonce_field() function:
<form method="post"> <!-- some inputs here ... --> <?php wp_nonce_field( 'name_of_my_action','name_of_nonce_field' ); ?> </form>
Then in the page where the form submits to, you can verify whether or not the form was submitted and update values if it was successfully submitted:
<?php
// if this fails, check_admin_referer() will automatically print a "failed" page and die.
if ( !empty($_POST) && check_admin_referer( 'name_of_my_action', 'name_of_nonce_field' ) ) {
// process form data, e.g. update fields
}
// Display the form
Since: 1.2.0
check_admin_referer() is located in wp-includes/pluggable.php.
Nonce functions: wp_explain_nonce(), wp_nonce_ays(), wp_nonce_field(), wp_nonce_ur(), wp_verify_nonce(), wp_create_nonce(), check_admin_referer(), check_ajax_referer(), wp_referer_field()
Nonce hooks: nonce_life, nonce_user_logged_out, explain_nonce_(verb)-(noun), check_admin_referer