简体   繁体   English

在 wordpress 中使用 auth_cookie_expiration 使 cookie 超时,但不适用于管理员

[英]Using auth_cookie_expiration in wordpress to time out cookie but not for administrator

I wonder if you could help me with something.我想知道你能不能帮我做点什么。 The following piece of code works in that after 30 seconds of logging in (yes its just for testing) users are logged out, but the administrator isn't.以下代码的工作原理是在登录 30 秒后(是的,它只是为了测试)用户被注销,但管理员没有。

function logout_after_time( $expiration, $user_id) {
    if(!user_can($user_id, 'update_plugins') ){
        $expiration = 30; // yes this is 30 seconds for testing
    }
    return $expiration;
}
add_filter('auth_cookie_expiration','logout_after_time', 10, 2);

However if I change that to the following:但是,如果我将其更改为以下内容:

function logout_after_time( $expiration, $user_id) {
        if(!current_user_can($user_id, 'administrator') ){
            $expiration = 30; // yes this is 30 seconds for testing
        }
        return $expiration;
    }
    add_filter('auth_cookie_expiration','logout_after_time', 10, 2);

It logs out all users.它注销所有用户。 I can't for the life of me think why this is?我不能为我的生活想这是为什么? Ideally I don't really want to check for caps, I would like to check for a role.理想情况下,我真的不想检查上限,我想检查角色。

Any ideas what I am doing wrong?任何想法我做错了什么? Thanks for all those who can help:)感谢所有可以提供帮助的人:)

Using current_user_can should not be used to check for roles, see the documentation :不应使用current_user_can来检查角色,请参阅文档

Passing role names to current_user_can() is discouraged as this is not guaranteed to work correctly (see #22624)不鼓励将角色名称传递给 current_user_can(),因为这不能保证正常工作(请参阅 #22624)

What you can do to check for a role though, is see if one role is in the array of roles of the current user:但是,您可以做些什么来检查角色,看看一个角色是否在当前用户的角色数组中:

$currentUser = wp_get_current_user();
if ( !in_array( 'administrator', (array) $user->roles ) ) {
    // Do something for users that don't have to role ' administrator'. 
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM