【问题标题】:WP Admin bar hidden on front-end?WP管理栏隐藏在前端?
【发布时间】:2019-11-28 09:51:30
【问题描述】:

我正在使用以下代码为特定用户隐藏 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);
}

它适用于subscribersvisitors,但是当以administrator 身份登录时,管理栏不会显示在前端。谁能告诉我我做错了什么?

解决方案:上面的代码有效

【问题讨论】:

  • 您在哪个函数中应用此代码?
  • 我刚刚将它粘贴到我的function.php 文件中

标签: php wordpress admin toolbar user-roles


【解决方案1】:

试试,

//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);
}

【讨论】:

  • 使用此功能,访问者会显示管理栏
  • 看起来像您创建的访问者角色的问题。此外,钩子“after_setup_theme”看起来不正确。您可以删除它并直接从 if 条件中放置代码或更改钩子。
  • 我也是这么想的但是怎么解决哈哈
  • 我更改了我的代码,但结果是一样的。我应该使用!is_user_logged_in() 吗?
  • 将我的代码更改为现在的样子,看起来这就是解决方案;)
【解决方案2】:

检查您的用户设置中是否选中了查看网站时显示工具栏

【讨论】:

    【解决方案3】:

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

    /**
     * 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;
        }
    }
    

    参考: https://cpothemes.com/disable-wordpress-admin-bar

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-26
      • 1970-01-01
      相关资源
      最近更新 更多