我建议如下:
默认情况下,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:从用户列表中隐藏特定用户如果要从用户列表中选择要隐藏的单个ID,请使用此选项(替换\'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