nonce_user_logged_out
云策文档标注
概述
nonce_user_logged_out 是一个 WordPress 过滤器钩子,用于控制生成 nonce 的用户是否被视为已登出状态。它允许开发者根据用户 ID 和 nonce 动作自定义 nonce 的验证逻辑。
关键要点
- 过滤器钩子:nonce_user_logged_out,用于修改生成 nonce 的用户 ID,特别是针对已登出用户(用户 ID 为 0)。
- 参数:接受两个参数:$uid(整数,表示用户的 ID)和 $action(字符串或整数,表示 nonce 的动作)。
- 应用场景:常用于 wp_verify_nonce() 和 wp_create_nonce() 函数中,以增强 nonce 的安全性和灵活性。
- 版本历史:自 WordPress 3.5.0 版本引入。
代码示例
/**
* Custom function to modify the nonce value for logged-out users.
*
* @param int $uid The user ID (0 for logged-out users).
* @param string $action The nonce action.
*
* @return int The modified user ID.
*/
function wpdocs_modify_nonce_for_logged_out_users( $uid, $action ) {
// Check if the user is logged out (uid = 0) and the specific nonce action.
if ( 0 === $uid && 'my_custom_action' === $action ) {
// Generate a modified nonce for logged-out users.
$uid = 999; // Example: use a specific user ID for logged-out users.
}
return $uid;
}
add_filter( 'nonce_user_logged_out', 'wpdocs_modify_nonce_for_logged_out_users', 10, 2 );
原文内容
Filters whether the user who generated the nonce is logged out.
Parameters
$uidint-
ID of the nonce-owning user.
$actionstring|int-
The nonce action, or -1 if none was provided.
Source
$uid = apply_filters( 'nonce_user_logged_out', $uid, $action );
Changelog
| Version | Description |
|---|---|
| 3.5.0 | Introduced. |
Skip to note 2 content
NANI SAMIREDDY
/** * Custom function to modify the nonce value for logged-out users. * * @param int $uid The user ID (0 for logged-out users). * @param string $action The nonce action. * * @return int The modified user ID. */ function wpdocs_modify_nonce_for_logged_out_users( $uid, $action ) { // Check if the user is logged out (uid = 0) and the specific nonce action. if ( 0 === $uid 0 && 'my_custom_action' === $action ) { // Generate a modified nonce for logged-out users. $uid = 999; // Example: use a specific user ID for logged-out users. } return $uid; } add_filter( 'nonce_user_logged_out', 'wpdocs_modify_nonce_for_logged_out_users', 10, 2 );