Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WP管理栏隐藏在前端?_Php_Wordpress_Admin_Toolbar_User Roles - Fatal编程技术网

Php WP管理栏隐藏在前端?

Php WP管理栏隐藏在前端?,php,wordpress,admin,toolbar,user-roles,Php,Wordpress,Admin,Toolbar,User Roles,我使用一个函数隐藏特定用户的WP管理栏,代码如下: //Hide admin bar for subscribers if (current_user_can('subscriber') || !is_user_logged_in() ) { // user can't view admin bar show_admin_bar(false); } else { show_admin_bar(true); } 它适用于订阅者和访问者,但当以管理员身份登录时,管理栏不会显

我使用一个函数隐藏特定用户的WP管理栏,代码如下:

//Hide admin bar for subscribers
if (current_user_can('subscriber') || !is_user_logged_in() ) {
    // user can't view admin bar
    show_admin_bar(false);
}
else {
    show_admin_bar(true);
}
它适用于
订阅者
访问者
,但当以
管理员身份登录时,管理栏不会显示在前端。谁能告诉我我做错了什么

解决方案:上面的代码可以工作

试试看

//Hide admin bar for subscribers
if( current_user_can('subscriber') || current_user_can('visitor') ) {
  // user can't view admin bar
  show_admin_bar(false);
} else {
  show_admin_bar(true);
}

更新代码:

//Hide admin bar for all users except administrators
if( current_user_can('manage_options') ) {
  show_admin_bar(true);
} else {
  // All other users can't view admin bar
  show_admin_bar(false);
}

检查查看站点时显示工具栏是否已在用户设置中选中。

使用
显示管理栏
过滤器隐藏/显示管理栏

/**
 * Checks if the user belongs to the roles.
 * 
 * @param int/WP_User $user Either user_id or WP_User object.
 * @param string/string[] $roles Single roles or array of roles.
 */
function is_user_in_role($user, $roles ) {
    // Set user_id to null;
    $user_obj = null;

    // Check if the $user is integer.
    if ( is_int( $user ) ) {
        $user_obj = get_user_by( 'id', $user );
    }

    // Check if the $user is object.
    if ( $user instanceof WP_User) {
        $user_obj = $user;
    }

    // Bail if the $user_id is not set.
    if ( null === $user_obj) {
        return false;
    }

    // Check if the user belons to the role.
    if ( is_string( $roles ) ) {
        return in_array( $roles, (array) $user_obj->roles );
    }

    // Check if the user belongs to the roles.
    if ( is_array( $roles ) ) {
        $user_belong_to = true;
        foreach( $roles as $role ) {
            if ( ! in_array( $role, (array) $user_obj->roles ) ) {
                $user_belong_to = false;
            }
        }
        return $user_belong_to;
    }

    // Return false if nothing works.
    return false;
}

add_filter( 'show_admin_bar', 'hide_admin_bar' );
function hide_admin_bar() {
    $user = wp_get_current_user();
    if ( is_user_in_role($user, 'administrator' ) ) {
        return false;
    } else {
        return true;
    }
}
参考:

使用此功能,将为访问者显示管理栏,如您创建的访问者角色问题。此外,挂钩“设置后主题”看起来不正确。你可以删除它,然后直接从条件中放入代码,或者更改钩子。这也是我的想法,但如何修复它hahaI更改了我的代码,但结果是一样的。我是否应该使用
!\u user\u logged\u in()
?将我的代码更改为现在的代码,看起来这就是解决方案;)在哪个函数中应用此代码?我刚刚将其粘贴到我的
function.php
文件中