Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am making system where I am delegating a lot of functionality to the front end of a system using Wordpress. I have listed all the roles (most of the custom) and need to ability for a logged in user to edit the name of the role or delete it.

I cant find any normal WP functions to do this, or plugins that allow the simple solution I need, so I was thinking I may need to run a MYSQL query to change the info but.. have you seen the wp_options table with the user role information?? Its a massive mess and I dont really want to destroy the system.

Any suggestions?

Thanks in advance.

share|improve this question

3 Answers 3

up vote 4 down vote accepted

There's a whole host of functions specifically for this purpose;

http://codex.wordpress.org/Function_Reference#User_and_Author_Functions

Of particular interest (but not limited to) are,

add_cap
add_role
get_role
map_meta_cap
remove_cap
remove_role

As well as numerous other user related functions that will allow you to verify/validate their authority based upon your use-case scenario and so on.

Looking in wp-includes/capabilities.php you we can see that the role_names are held in an array, therefore something to the effect of,

add_action('init', 'update_role_name');
function update_role_name(){
    global $wp_roles;
    $wp_roles->roles['subscriber']['name'] = 'member'; 
    $wp_roles->role_names['subscriber'] = 'member'; 
}

Might need a little tweaking as that's untested.

Edit:

I just found this: is-there-way-to-rename-user-role-name-without-plugin - check it out. Virtually identical to what I am suggesting but with the exception of instantiating the class and checking that the variable $wp_roles is actually defined/set. I'll assume that it works on the basis that its marked correctly but naturally please test this to confirm.

But in a similar fashion looking at the following which is taken directly from core installation file wp-includes/capabilities.php line 133,

/**
 * Add role name with capabilities to list.
 *
 * Updates the list of roles, if the role doesn't already exist.
 *
 * The capabilities are defined in the following format `array( 'read' => true );`
 * To explicitly deny a role a capability you set the value for that capability to false.
 *
 * @since 2.0.0
 * @access public
 *
 * @param string $role Role name.
 * @param string $display_name Role display name.
 * @param array $capabilities List of role capabilities in the above format.
 * @return null|WP_Role WP_Role object if role is added, null if already exists.
 */
function add_role( $role, $display_name, $capabilities = array() ) {
    if ( isset( $this->roles[$role] ) )
        return;

    $this->roles[$role] = array(
        'name' => $display_name,
        'capabilities' => $capabilities
        );
    if ( $this->use_db )
        update_option( $this->role_key, $this->roles );
    $this->role_objects[$role] = new WP_Role( $role, $capabilities );
    $this->role_names[$role] = $display_name;
    return $this->role_objects[$role];
}

...we can see,

Updates the list of roles, if the role doesn't already exist.

add_role( $role, $display_name, $capabilities = array() ) 

...therefore updating the $display_name argument of an existing role will have the same desired effect without actually modifying the role itself so as to preserve its users associated with the role.

share|improve this answer
    
Yeah I have been using these and they work great, but if I need someone to edit the name of a role, I would remove and add the new named role, but that would delete the people in the role right? –  JamesG Nov 2 '12 at 12:11
    
@JamesG It won't delete anything if the role name is exactly the same as before. You can still change the display name (which is the one displayed to the user) –  Mridul Aggarwal Nov 2 '12 at 12:18
    
Small update. Untested, but should work. @MridulAggarwal what do you think on that matter...? –  userabuser Nov 2 '12 at 12:23
    
just one line added $wp_roles->role_objects['subscriber']->name = 'member';, otherwise great –  Mridul Aggarwal Nov 2 '12 at 12:39

Don't work with that mess

Open the file "wp-includes/capabilities.php"

Inside there are 3 classes "WP_Roles", "WP_Role" & "WP_User"

These 3 classes are capable of easily handling every functionality related to user roles & capabilities

share|improve this answer

If you’re looking for a plugin, you should try out the Members plugin by Justin Tadlock. It contains an easy and clean role manager.

I highly recommend to have a look at the code too. Justin’s code really is top-notch, a true delight in contrast with many other WP plugins.

Role manager Edit role

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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