admin管理员组文章数量:1296310
I used the snippet below to lock out all administrators and editors except myself
if ( is_user_logged_in() ){
$current_user = wp_get_current_user() ;
if ( in_array( 'administrator', (array) $current_user->roles ) || in_array( 'editor', (array) $current_user->roles )) {
if ($current_user->user_login != 'sheila' ){
wp_logout();
return new WP_Error( 'login_again_please', 'Please log in again' );
}
}
}
When I try to add a second user in the second if statement we are both locked out:
if ( is_user_logged_in() ){
$current_user = wp_get_current_user() ;
if ( in_array( 'administrator', (array) $current_user->roles ) || in_array( 'editor', (array) $current_user->roles )) {
if (($current_user->user_login != 'john' ) || ($current_user->user_login != 'sheila' )){
wp_logout();
return new WP_Error( 'login_again_please', 'Please log in again' );
}
}
}
How do I fix the above snippet to allow two administrators/editors with specific usernames to login or can I have an alternative with the same outcome?
I used the snippet below to lock out all administrators and editors except myself
if ( is_user_logged_in() ){
$current_user = wp_get_current_user() ;
if ( in_array( 'administrator', (array) $current_user->roles ) || in_array( 'editor', (array) $current_user->roles )) {
if ($current_user->user_login != 'sheila' ){
wp_logout();
return new WP_Error( 'login_again_please', 'Please log in again' );
}
}
}
When I try to add a second user in the second if statement we are both locked out:
if ( is_user_logged_in() ){
$current_user = wp_get_current_user() ;
if ( in_array( 'administrator', (array) $current_user->roles ) || in_array( 'editor', (array) $current_user->roles )) {
if (($current_user->user_login != 'john' ) || ($current_user->user_login != 'sheila' )){
wp_logout();
return new WP_Error( 'login_again_please', 'Please log in again' );
}
}
}
How do I fix the above snippet to allow two administrators/editors with specific usernames to login or can I have an alternative with the same outcome?
Share Improve this question edited Apr 5, 2021 at 5:12 Nigel Rodgers asked Apr 4, 2021 at 16:27 Nigel RodgersNigel Rodgers 236 bronze badges 3 |2 Answers
Reset to default 1||
means OR.
Your code will never allow $current_user to proceed because one of the OR conditions will fail.
Try using AND &&
instead:
if ( ($current_user->user_login != 'john') && ($current_user->user_login != 'sheila' ) ) {
Looks like it would work! I ended up using something like this:
if( !($some_variable === 'uk' || $another_variable === 'in')){
//this occurs when neither of the above are true
}
courtesy of a comment by @Habchi on this thread: https://stackoverflow/questions/19949923/simpler-way-to-check-if-variable-is-not-equal-to-multiple-string-values I'll post the full solution later if someone doesn't beat me to it.
本文标签: loginLock out all WordPress Administrators except two specific users
版权声明:本文标题:login - Lock out all WordPress Administrators except two specific users 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741634236a2389548.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
)
before your{
however you only have one. – Anake.me Commented Apr 5, 2021 at 5:03