admin管理员组

文章数量:1122846

I have authors on my site and I want them to be able to add/edit/remove users. I can see how I can add the relevant capabilities to the author role with get_role and add_cap. But, this does not actually add the relevant menu items to the main left hand menu in Admin. (If logged in as an Admin I see the attached. How do I make this (and the pages) appear for authors, now that they actually have the relevant capabilities?

Thank you

I have authors on my site and I want them to be able to add/edit/remove users. I can see how I can add the relevant capabilities to the author role with get_role and add_cap. But, this does not actually add the relevant menu items to the main left hand menu in Admin. (If logged in as an Admin I see the attached. How do I make this (and the pages) appear for authors, now that they actually have the relevant capabilities?

Thank you

Share Improve this question asked Sep 4, 2024 at 14:03 Justin WylllieJustin Wylllie 374 bronze badges 1
  • are you sure they have all the needed capabilities? Which capabilities specifically did you try to add? And what code did you use to do it? When/where did it run? The capability for listing users is not the same as the capability for adding/editing/removing users – Tom J Nowell Commented Sep 4, 2024 at 14:42
Add a comment  | 

1 Answer 1

Reset to default 0

How do I make this (and the pages) appear for authors, now that they actually have the relevant capabilities?

They don't have the needed capabilities, just because a role can add/edit/remove users does not mean they can list_users.

Similarly there are other capabilities such as role promotion etc, here's the code for administrators, e.g. this is what the code in WordPress core looks like:

    $role = get_role( 'administrator' );

    if ( ! empty( $role ) ) {
        $role->add_cap( 'update_core' );
        $role->add_cap( 'list_users' );
        $role->add_cap( 'remove_users' );
        $role->add_cap( 'promote_users' );

and if you look in wp-admin/menu.php you'll see where it adds the user menus:


if ( current_user_can( 'list_users' ) ) {
    $menu[70] = array( __( 'Users' ), 'list_users', 'users.php', '', 'menu-top menu-icon-users', 'menu-users', 'dashicons-admin-users' );
} else {
    $menu[70] = array( __( 'Profile' ), 'read', 'profile.php', '', 'menu-top menu-icon-users', 'menu-users', 'dashicons-admin-users' );
}

if ( current_user_can( 'list_users' ) ) {
    $_wp_real_parent_file['profile.php'] = 'users.php'; // Back-compat for plugins adding submenus to profile.php.
    $submenu['users.php'][5]             = array( __( 'All Users' ), 'list_users', 'users.php' );
    if ( current_user_can( 'create_users' ) ) {
        $submenu['users.php'][10] = array( __( 'Add New User' ), 'create_users', 'user-new.php' );
    } elseif ( is_multisite() ) {
        $submenu['users.php'][10] = array( __( 'Add New User' ), 'promote_users', 'user-new.php' );
    }

    $submenu['users.php'][15] = array( __( 'Profile' ), 'read', 'profile.php' );
} else {
    $_wp_real_parent_file['users.php'] = 'profile.php';

Capabilities added by core have some oddness like this where you can add/edit/create/remove things but unless you can read/list/view them those capabilities are useless, and it doesn't always make immediate sense due to backwards compatibility. Always double check and if in doubt add all capabilities that seem related and then whittle them down, or check the code for core.

本文标签: capabilitiesHow can I add the User Menu for Authors (role)