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?
-
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 at 14:42
1 Answer
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.
-
Thanks - however. It turned out the menu items appeared simply by adding the correct capabilities to the role. list_users and create_users seemed to be the key ones. I didn't need to add the menu separately from this. Commented Sep 5 at 15:13
-
that's what my answer says, the code block isn't a suggested solution it's a copy of what's in core to help explain things– Tom J Nowell ♦Commented Sep 5 at 15:59
-
Ah. Ok. Thanks. I see, as you said "you'll see where it adds the menus". Commented Sep 9 at 12:44