doujiao6116 2013-04-01 18:55
浏览 16
已采纳

多个用户的维护模式功能 - php数组无法正常工作?

I have created a maintenance mode function, but I can only get it to work for one username.

get_currentuserinfo();

global $current_user;

// MAINTAINANCE MODE
function website_site_maintenance() {
    global $current_user;
    if ( 'josh' != $current_user->user_login ) {
       // vars
       $logout_url = wp_login_url().'?mode=maintenance';
       wp_logout();
       wp_redirect( $logout_url, 302 );
    }     
}
add_action('get_header', 'website_site_maintenance');
add_action('admin_init', 'website_site_maintenance');


// CUSTOM LOGIN MESSAGES
function website_my_login_message() {

    if( $_GET['mode'] == 'maintenance' ){
        $message = '<p class="message"><b>Site undergoing maintenance.</b></p>';
        return $message;
    }

}
add_filter('login_message', 'website_my_login_message');

this above function works, but I want to add more maintenance users.

So i've tried this but it does not work...

// MAINTAINANCE MODE
function website_site_maintenance() {
    global $current_user;
    $maintenance_users = array('josh','george','bob');
    if ( $maintenance_users != $current_user->user_login ) {
       // vars
       $logout_url = wp_login_url().'?mode=maintenance';
       wp_logout();
       wp_redirect( $logout_url, 302 );
    }     
}

I simply tried to add the users into an array.

$maintenance_users = array('josh','george','bob');

But this doesn't seem to work as it should.

  • 写回答

1条回答 默认 最新

  • dongtiao0657 2013-04-01 19:10
    关注

    Instead of using

    if ( $maintenance_users != $current_user->user_login ) {
       // vars
       $logout_url = wp_login_url().'?mode=maintenance';
       wp_logout();
       wp_redirect( $logout_url, 302 );
    }
    

    use

    if ( !in_array($current_user->user_login, $maintenance_users) ) {
       // vars
       $logout_url = wp_login_url().'?mode=maintenance';
       wp_logout();
       wp_redirect( $logout_url, 302 );
    }
    

    The problem you're having is that you're comparing an array to a string, and this is not going to work. In the "correct" version you'll be checking whether the username of the currently logged in user is in the array $maintenance_users you specified. Hope that helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 图书信息管理系统程序编写
  • ¥15 7-1 jmu-java-m02-使用二维数组存储多元线性方程组
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题