admin管理员组文章数量:1122846
I have added some extra capabilities to the existing 'editor' role in wordpress with:
function add_uedit_caps() {
$role = get_role('editor');
$role->add_cap('create_users');
$role->add_cap('list_users');
$role->add_cap('add_users');
$role->add_cap('edit_users');
}
add_action('admin_init', 'add_uedit_caps');
This piece works fine.
ISSUE:
The above code works to list and view all types of users by the 'editor'. But if a new user added by the 'editor', it takes up the 'new user default role' set up in wordpress (reference), ie the role set in settings > general. No role change option is available too in the user edit mode.
I want the 'editors' to list/view/add/edit only the users with the role 'author'. How can I achieve this through code (a method or class with action) and WITHOUT any plugin or tampering the core files?
I have added some extra capabilities to the existing 'editor' role in wordpress with:
function add_uedit_caps() {
$role = get_role('editor');
$role->add_cap('create_users');
$role->add_cap('list_users');
$role->add_cap('add_users');
$role->add_cap('edit_users');
}
add_action('admin_init', 'add_uedit_caps');
This piece works fine.
ISSUE:
The above code works to list and view all types of users by the 'editor'. But if a new user added by the 'editor', it takes up the 'new user default role' set up in wordpress (reference), ie the role set in settings > general. No role change option is available too in the user edit mode.
I want the 'editors' to list/view/add/edit only the users with the role 'author'. How can I achieve this through code (a method or class with action) and WITHOUT any plugin or tampering the core files?
Share Improve this question asked Nov 27, 2020 at 18:00 sariDonsariDon 2651 gold badge2 silver badges18 bronze badges2 Answers
Reset to default 1In addition to the code piece in the question,
1. To display only the author roles in the user list page of editor:
add_action('pre_user_query','editors_edit_author_list');
function editors_edit_author_list($user_search) {
$user = wp_get_current_user();
if($user->ID != 1) {
$user_meta=get_userdata($user->ID);
//$user_roles= $user_meta->roles;
global $wpdb;
if(in_array('editor', $user_meta->roles))
{
$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 LIKE '%author%')",
$user_search->query_where
);
}
else
$user_search->query_where = str_replace('WHERE 1=1', "WHERE 1=1 AND {$wpdb->users}.ID<>1", $user_search->query_where);
}
}
2. Make editors only edit the authors:
add_filter('editable_roles', array($this, 'editor_editable_roles'));
function editor_editable_roles($roles)
{
$user = wp_get_current_user();
$user_meta=get_userdata($user->ID);
if(in_array('editor', $user_meta->roles)) {
$tmp = array_keys($roles);
foreach($tmp as $r) {
if('author' == $r)
continue;
unset($roles[$r]);
}
}
return $roles;
}
3. Change the users added by the editor to author role (from the 'new user default role'):
add_action( 'profile_update', 'process_editor_added_user', 10, 1 );
add_action( 'user_register', 'process_editor_added_user', 10, 1 );
function process_editor_added_user($user_id)
{
$user = wp_get_current_user();
if($user->ID != $user_id)
{
$u = new WP_User($user_id);
$u->remove_role('subscriber');
$u->add_role('author');
}
}
There is an alternative to point #3 by hijacking the default new user role. I have not tested this:
add_filter('pre_option_default_role', function($default_role){
return 'author';
});
That's seems to be all.
If you don't follow OOP patterns then the @sariDon code has to be adjusted a bit in point 2 (dont't need $this, just call the function). Don't have enough reputation to comment so I have to post a new answer.
So the fully working piece of code in functions.php would be
// grant roles
add_action('admin_init', 'add_uedit_caps');
function add_uedit_caps() {
$role = get_role('editor');
$role->add_cap('create_users');
$role->add_cap('list_users');
$role->add_cap('add_users');
$role->add_cap('edit_users');
}
// display only the author roles in the user list page of editor
add_action('pre_user_query','editors_edit_author_list');
function editors_edit_author_list($user_search) {
$user = wp_get_current_user();
if($user->ID != 1) {
$user_meta=get_userdata($user->ID);
//$user_roles= $user_meta->roles;
global $wpdb;
if(in_array('editor', $user_meta->roles)) {
$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 LIKE '%author%')",
$user_search->query_where
);
} else {
$user_search->query_where = str_replace('WHERE 1=1', "WHERE 1=1 AND {$wpdb->users}.ID<>1", $user_search->query_where);
}
}
// Make editors only edit the authors
add_filter('editable_roles', 'editor_editable_roles');
function editor_editable_roles($roles) {
$user = wp_get_current_user();
$user_meta=get_userdata($user->ID);
if(in_array('editor', $user_meta->roles)) {
$tmp = array_keys($roles);
foreach($tmp as $r) {
if('author' == $r) {
continue;
unset($roles[$r]);
}
}
}
return $roles;
}
// Change the users added by the editor to author role (from the 'new user default role')
add_action( 'profile_update', 'process_editor_added_user', 10, 1 );
add_action( 'user_register', 'process_editor_added_user', 10, 1 );
function process_editor_added_user($user_id) {
$user = wp_get_current_user();
if($user->ID != $user_id) {
$u = new WP_User($user_id);
$u->remove_role('subscriber');
$u->add_role('author');
}
}
本文标签: How to make WordPress 39editor39 role to listviewaddedit users only with the role 39author39
版权声明:本文标题:How to make WordPress 'editor' role to listviewaddedit users only with the role 'author'? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736296936a1929905.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论