check_admin_referer()
Проверено на WordPress 6.9, обновлено Источник: WordPress Developer Resources.
Сигнатура
check_admin_referer( int|string $action = -1, string $query_arg = '_wpnonce' ): int|false
Описание
Эта функция подтверждает, что пользователь намеревается выполнить заданное действие, что помогает защититься от атак типа clickjacking. Она проверяет намерение, а не права доступа, поэтому не проверяет права пользователя. Это следует делать с помощью current_user_can() или аналогичной функции.
Если значение одноразового кода недопустимо, функция завершит работу сообщением в стиле «Вы уверены?».
Оригинал (английский)
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.
Параметры
$action
int|string
необязательный
= -1
$query_arg
string
необязательный
= '_wpnonce'
Возвращаемое значение
int|false
Исходный код
wp-includes/pluggable.php:1372
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;
}
История изменений
| Версия | Описание |
|---|---|
| 2.5.0 | The $query_arg parameter was added. |
| 1.2.0 | Introduced. |

