• Resolved John Sundberg

    (@bhwebworks)


    Just noticed that adding a new user with “Multiple Roles” activated results in that new user having no role, even though one was selected during the add new user process.

    I traced the problem to update_roles in model.php, where the existing role(s) is/are removed and the new array of roles is added to that user. However, during the add new user process the “Multiple Roles” checklist is not available, and when that user is added their only role is removed but is not replaced.

    Adding the following code to update_roles checks to see if this is the add new user process, and if so, it doesn’t follow through with removing the new user’s role.

    global $hook_suffix;
    if ( 'user-new.php' === $hook_suffix )
    	return;

    So the entire function looks like this:

    public function update_roles( $user_id = 0, $roles = array() ) {
    
    	global $hook_suffix;
    	if ( 'user-new.php' === $hook_suffix )
    		return;
    
    	$roles = array_map( 'sanitize_key', (array) $roles );
    	$user = get_user_by( 'id', $user_id );
    
    	// remove all roles
    	$user->set_role( '' );
    
    	foreach( $roles as $role ) {
    		$user->add_role( $role );
    	}
    
    }

    I tried a few different ways of solving this issue, and this method seems to be the simplest and most efficient.

    John

    https://wordpress.org/plugins/multiple-roles/

  • The topic ‘Add new user results in no role for that user’ is closed to new replies.