0

I created a new user role called "developer" and I gave it all of administrator's capabilities:

$admin_role = get_role( 'administrator' );
add_role( 'developer', __( 'Developer' ), $admin_role->capabilities );

This works great except I'd like Developers to be able to add/edit Administrator users. However, when I add a new user, "Administrator" isn't one of the options in the user role dropdown list.

I tried having the editable_roles filter return all of the roles in the system, so that the Developer can add/edit all roles:

add_filter( 'editable_roles', 'allow_developers_to_add_administrators' );
function allow_developers_to_add_administrators( $roles ){
  $user = wp_get_current_user();
  if( in_array( 'developer', $user->roles )){
    return array_keys( $GLOBALS['wp_roles']->roles );
  }
  return $roles;
}

But when I do that, the roles dropdown list only shows the 1st letter of each role (they have a custom set of roles and this is showing the first letter for each one):

enter image description here

I also tried simply appending 'administrator' to the $roles returned from the editable_roles filter, but it - predictably - just added an item with 'a' to the dropdown list.

Anyone know how I can make this work? (Note that I'm not on multisite so I don't think there's a super admin role I can hook into here)

Thanks!
Abby

1 Answer 1

0

Make sure you're adding and removing roles on plugin activation/deactivation.

add_role() only adds a role if it doesn't already exist. So if you already added the developer role without the capability to add administrators, calling add_role() again won't re-add the role. You have to remove the role first.

<?php
/**
 * Plugin Name: Stackexchange Sample
 */

//* Don't access this file directly
defined( 'ABSPATH' ) or die();

//* Add role for developer that has the same capabilities as administrators
register_activation_hook( __FILE__ , 'wpse_106269_activation' );
function wpse_106269_activation() {
  $admin_role = get_role( 'administrator' );
  add_role( 'developer', __( 'Developer' ), $admin_role->capabilities );
}

//* Remove developer role
register_deactivation_hook( __FILE__ , 'wpse_106269_deactivation' );
function wpse_106269_activation() {
  remove_role( 'developer' );
}

Deactivate and re-activate your plugin and the developer role should have the same capabilities as the administrator role.

3
  • Thanks! I am doing that - just simplified the code down for sake of sharing. Any ideas on how to enable the Developer to add/edit Administrators? Feb 8, 2017 at 1:29
  • I tested the above code on a fresh install with no plugins and default theme and it added the Developer role with the capability to add/edit/remove administrators. (Actually, the Developer role is exactly the same as the Administrator role.) Feb 8, 2017 at 1:50
  • huh, interesting. I'll try again! Feb 9, 2017 at 4:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.