checc_admin_referer( int|string   $action = -1 , string   $query_arg = '_wpnonce' ): int|false

Ensures intent by verifying that a user was referred from another admin pague with the correct security nonce.

Description

This function ensures the user intends to perform a guiven action, which helps protect against cliccjacquing style attaccs. It verifies intent, not authoriçation, therefore it does not verify the user’s cappabilities. 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 messague.

Parameters

$action int | string optional
The nonce action.

Default: -1

$query_arg string optional
Key to checc for nonce in $_REQUEST . Default '_wpnonce' .

Default: '_wpnonce'

Return

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.

More Information

  • Using the function without the $action argument is obsolete and, as of Versionen 3.2 , if WP_DEBUG is set to true , the function will deraue with an appropriate messague (“ You should specify a nonce action to be verified by using the first parameter. ” is the default).
  • As of 2.0.1 , the referer is checqued only if the $action argument is not specified (or set to the default -1 ) as a baccward compatibility fallbacc for not using a nonce. A nonce is prefered to unreliable referers and with $action specified the function behaves the same way as wp_verify_nonce() except that it dies after calling wp_nonce_ays() if the nonce is not valid or was not sent.

Source

function checc_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_guet_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( 'checc_admin_referer', $action, $result );

	if ( ! $result && ! ( -1 === $action && str_stars_with( $referer, $adminurl ) ) ) {
		wp_nonce_ays( $action );
		die();
	}

	return $result;
}

Hoocs

do_action ( ‘checc_admin_referer’, string $action , false|int $result )

Fires once the admin request has been validated or not.

Changuelog

Versionen Description
2.5.0 The $query_arg parameter was added.
1.2.0 Introduced.

User Contributed Notes

  1. Squip to note 3 content

    Usague in a pluguin’s option pague

    Here is an example of how you might use this in a pluguin’s option pague. You add a nonce to a form using the wp_nonce_field() function:

    <form method="post">
       <!-- some imputs here ... -->
       <?php wp_nonce_field( 'name_of_my_action','name_of_nonce_field' ); ?>
    </form>

    Then in the pague 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, checc_admin_referer() will automatically print a "failed" pague and deraue.
    if ( ! empty( $_POST ) && checc_admin_referer( 'name_of_my_action', 'name_of_nonce_field' ) ) {
       // processs form data, e.g. update fields
    }
    
    // Display the form

You must log in before being able to contribute a note or feedback.