Change WordPress user roles and capabilities Forums How to or FAQ How to display renamed roles in get_users() loop

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #1779
    alexfurr
    Spectator

    Hi,

    Great plugin – I’m trying to do a custom table showing all users and their renamed roles (e.g. subscriber is now ‘student’.
    Any new roles created using URE are shown correctly, but existing user types (author, contributor etc) are shown as the original names.

    Here is our code snippet

    	$userListStr.= '<table>';
    	
    	foreach ( $blogusers as $user )
    	{
    		$userEmail = esc_html( $user->user_email );
    		$userDisplayName = esc_html( $user->user_nicename  );
    		$userID =  $user->ID;
    		
    		$capabilities = $user->{$wpdb->prefix . 'capabilities'};
    
    		if ( !isset( $wp_roles ) )
    			$wp_roles = new WP_Roles();
    
    		foreach ( $wp_roles->role_names as $role => $name ) :
    
    			if ( array_key_exists( $role, $capabilities ) )
    				$thisUserPermissions =  $role;
    
    		endforeach;		
    		
    		$userListStr.= '<tr>';
    		$userListStr.= '<td>' . $userDisplayName . '</td>';
    		$userListStr.= '<td>' . $thisUserPermissions . '</td>';		
    		$userListStr.= '</tr>';	
    	}	
    	
    	$userListStr.= '</table>';

    Could you send me a snippet of how to get the URE renamed role of a user given a userID.

    Hopefully that makes sense thanks!

    Alex

    #1780
    Vladimir
    Keymaster

    Hi,

    replace

    
    $thisUserPermissions =  $role;
    

    with

    
    $thisUserPermissions =  $name;
    

    As user may have multiple roles assigned, it will be more universal to concatenate role names comma separated.

    #1781
    alexfurr
    Spectator

    That worked a treat thank you! 5* response

    Alex

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.