Display role name on my_account page WooCommerce

323 Views Asked by At

I use this code in functions.php to display user role in woocommerce My-Account page (Welcome user...etc.):

function get_user_role() {
    global $current_user;

    $user_roles = $current_user->roles;
    $user_role = array_shift($user_roles);

    return $user_role;
}

But it displays only user role ID, without name. How can this code be modified to display USER ROLE NAME instead?

Thanx in advance.

1

There are 1 best solutions below

1
Justin R. On

Try this. It returns the translated role name of the current user.

function se_get_current_user_role() {
global $wp_roles;

$current_user = wp_get_current_user();
$roles = $current_user->roles;
$role = array_shift( $roles );

return isset( $wp_roles->role_names[ $role ] ) ? translate_user_role( 
$wp_roles->role_names[ $role ] ) : FALSE;
}