阻止管理员用户编辑/查看用户列表中的超级管理员

时间:2013-06-30 作者:Carol

如何从用户列表中隐藏我的超级管理员?我的客户端必须能够创建用户,但不能编辑/查看我的超级管理员用户。这可能吗?

2 个回复
SO网友:Jentan Bernardus

我建议如下:

默认情况下,WordPress中的编辑器用户角色无法编辑用户。您可能希望为保留为“编辑器”的客户端添加该功能。这将允许编辑器添加新用户(创建新用户)、删除用户和编辑用户。这是通过使用\'add_cap\'.

这允许编辑器管理用户

function wpse104808_editor_manage_users() {

    if ( get_option( \'wpse104808_add_cap_editor_once\' ) != \'done\' ) {

        // let editor manage users

        $edit_editor = get_role(\'editor\'); // Get the user role
        $edit_editor->add_cap(\'edit_users\');
        $edit_editor->add_cap(\'list_users\');
        $edit_editor->add_cap(\'promote_users\');
        $edit_editor->add_cap(\'create_users\');
        $edit_editor->add_cap(\'add_users\');
        $edit_editor->add_cap(\'delete_users\');

        update_option( \'wpse104808_add_cap_editor_once\', \'done\' );
    }

}
add_action( \'init\', \'wpse104808_editor_manage_users\' );
阻止编辑器删除管理员用户这将阻止编辑器删除、编辑或添加新管理员

class wpse104808_user_caps {

  // Add our filters
  function wpse104808_user_caps(){
    add_filter( \'editable_roles\', array(&$this, \'editable_roles\'));
    add_filter( \'map_meta_cap\', array(&$this, \'map_meta_cap\'),10,4);
  }
  // Remove \'Administrator\' from the list of roles if the current user is not an admin
  function editable_roles( $roles ){
    if( isset( $roles[\'administrator\'] ) && !current_user_can(\'administrator\') ){
      unset( $roles[\'administrator\']);
    }
    return $roles;
  }
  // If someone is trying to edit or delete an
  // admin and that user isn\'t an admin, don\'t allow it
  function map_meta_cap( $caps, $cap, $user_id, $args ){
    switch( $cap ){
        case \'edit_user\':
        case \'remove_user\':
        case \'promote_user\':
            if( isset($args[0]) && $args[0] == $user_id )
                break;
            elseif( !isset($args[0]) )
                $caps[] = \'do_not_allow\';
            $other = new WP_User( absint($args[0]) );
            if( $other->has_cap( \'administrator\' ) ){
                if(!current_user_can(\'administrator\')){
                    $caps[] = \'do_not_allow\';
                }
            }
            break;
        case \'delete_user\':
        case \'delete_users\':
            if( !isset($args[0]) )
                break;
            $other = new WP_User( absint($args[0]) );
            if( $other->has_cap( \'administrator\' ) ){
                if(!current_user_can(\'administrator\')){
                    $caps[] = \'do_not_allow\';
                }
            }
            break;
        default:
            break;
    }
    return $caps;
  }

}

$wpse104808_custom_role = new wpse104808_user_caps();
从用户列表中隐藏管理员以下是从用户列表中隐藏用户的3种变体。选择以下选项之一

选项1:从用户列表中隐藏特定用户2,5,7,9\' 要隐藏的用户ID)

add_action(\'pre_user_query\',\'wpse104808_pre_user_query\');
function wpse104808_pre_user_query($user_search) {

    $admin_ids = \'2,5,7,9\'; // REPLACE THESE NUMBERS WITH IDs TO HIDE.

    $user = wp_get_current_user();
    $admin_array = explode($admin_ids, \',\');
    if ( ! in_array( $user->ID, $admin_array ) ) {
        global $wpdb;
        $user_search->query_where = str_replace(\'WHERE 1=1\', "WHERE 1=1 AND {$wpdb->users}.ID NOT IN($admin_ids)",$user_search->query_where);

    }
}
选项2:从用户列表中隐藏所有管理员

// Hide all administrators from user list.

add_action(\'pre_user_query\',\'wpse104808_pre_user_query\');
function wpse104808_pre_user_query($user_search) {

    $user = wp_get_current_user();

    if ( ! current_user_can( \'manage_options\' ) ) {

        global $wpdb;

        $user_search->query_where = 
            str_replace(\'WHERE 1=1\', 
            "WHERE 1=1 AND {$wpdb->users}.ID IN (
                 SELECT {$wpdb->usermeta}.user_id FROM $wpdb->usermeta 
                    WHERE {$wpdb->usermeta}.meta_key = \'{$wpdb->prefix}capabilities\'
                    AND {$wpdb->usermeta}.meta_value NOT LIKE \'%administrator%\')", 
            $user_search->query_where
        );

    }
}
相关链接:Roles and Capabilities
-add cap
-WP_Role::add_cap
-WP_User::add_cap

SO网友:Krzysiek Dróżdż

我想这应该对您有所帮助(只需确保您隐藏了正确的用户):

Remove Ability for Other Users to View Administrator in User List?

再看看这个:

Remove Adminstrator Hyperlink from a user having role to add and see users

结束

相关推荐

正在尝试获取wp-includes/capabilities.php中非对象的属性

在调试中,我每分钟都会收到以下通知序列。日志:[23-Oct-2012 13:27:33 UTC] PHP Notice: Trying to get property of non-object in mysite/wp-includes/capabilities.php on line 1022 [23-Oct-2012 13:27:33 UTC] PHP Notice: Trying to get property of non-object in mysite/wp-includes/