I currently have this snippet:
$user = new WP_User(get_current_user_id());
echo $user->roles[1];
and the output the slug of the bbPress forum role. (roles[0] would be the general WP role but I don't need that.)
What I need is the role name, not the slug. So, the expected output should be something like "Keymaster", "Participant", "Spectator" etc.
So, how do I get the Role Name of the current user?
I currently have this snippet:
$user = new WP_User(get_current_user_id());
echo $user->roles[1];
and the output the slug of the bbPress forum role. (roles[0] would be the general WP role but I don't need that.)
What I need is the role name, not the slug. So, the expected output should be something like "Keymaster", "Participant", "Spectator" etc.
So, how do I get the Role Name of the current user?
Share Improve this question edited Sep 19, 2016 at 12:31 cjbj 15k16 gold badges42 silver badges89 bronze badges asked Sep 19, 2016 at 11:58 boosterbooster 211 gold badge1 silver badge3 bronze badges 1 |5 Answers
Reset to default 5I'm not sure if bbPress follows WordPress conventions, but WP has a global class called $WP-roles
that holds the role information. So, starting from what you have, there is the role of the current user:
$current_role = $user->roles[1];
Next, retrieve a list of all roles:
$all_roles = $wp_roles->roles;
Then, loop through $all_roles
and find the $current_role"
:
foreach ($all_roles as $role_key => $role_details) {
if ($role_key == $current_role) $current_role_name = $role_details['name'];
}
Now, $current_role_name
should hold the display name you're looking for (I didn't check this code, however).
thnx for this code snippet! I'm running a Buddypress install (and custom theme) where it is possible to add multiple roles to one user. I adjusted your code a little bit for that to work:
<?php
//Get role name by user ID
if( !function_exists('get_user_role_name') ){
function get_user_role_name($user_ID){
global $wp_roles;
$user_data = get_userdata($user_ID);
$user_role_slug = $user_data->roles;
foreach($user_role_slug as $user_role){
//count user role nrs
$user_role_nr++;
//add comma separation of there is more then one role
if($user_role_nr > 1) { $user_role_list .= ", "; }
//add role name
$user_role_list .= translate_user_role($wp_roles->roles[$user_role]['name']);
}
//return user role list
return $user_role_list;
}
}
?>
Maybe they've already solved this issue. But, now I just wrote this and I decided to share it.
<?php
//Get role name by user ID
if( !function_exists('get_user_role_name') ){
function get_user_role_name($user_ID){
global $wp_roles;
$user_data = get_userdata($user_ID);
$user_role_slug = $user_data->roles[0];
return translate_user_role($wp_roles->roles[$user_role_slug]['name']);
}
}
?>
<?php echo get_user_role_name(User ID here);?>
Here is the way to get it
function get_user_role($user_id) {
global $wp_roles;
$roles = array();
$user = new WP_User( $user_id );
if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
foreach ( $user->roles as $role )
$roles[] .= translate_user_role( $role );
}
return implode(', ',$roles);
}
//then call the function with userid param
echo get_user_role( 7 );
You can get current user role name(translatable name but not slug) by following function, you just need to pass current user role slug as a parameter:
function wp_get_current_user_translatable_role_name( $current_user_role_slug = '' ) {
$role_name = '';
if ( ! function_exists( 'get_editable_roles' ) ) {
require_once ABSPATH . 'wp-admin/includes/user.php';
}
// Please note that translate_user_role doesn't work in the front-end currently.
load_textdomain( 'default', WP_LANG_DIR . '/admin-' . get_locale() . '.mo' );
$editable_roles = array_reverse( get_editable_roles() );
foreach ( $editable_roles as $role => $details ) {
$name = translate_user_role( $details['name'] );
// preselect specified role
if ( $current_user_role_slug == $role ) {
$role_name = $name;
}
}
echo $role_name ;
}
Now, get current user role slug name by following code:
$user_meta = get_userdata(get_current_user_id());
$current_user_role_slug = $user_meta->roles[0];
Use wp_get_current_user_translatable_role_name( $current_user_role_slug );
function to get current user role name(translatable name but not slug).
NOTE: User must have to login to view role name.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742360940a4429379.html
wp_get_current_user()
should helps you. You get return all data to the user via the class ´WP_User`. – bueltge Commented Sep 19, 2016 at 12:31