Wordpress Role Display Name

陌路散爱 提交于 2019-11-29 07:21:23

If you are running a non-english WordPress site and want to display the proper (translated) Role name, as it appears in all WordPress administration pages, here's how to do it:

    global $wp_roles;
    echo translate_user_role( $wp_roles->roles[ $role ]['name'] );

where $role is the role name (i.e. 'special' in the original question).

Note: translate_user_role is a WordPress core not-so-documented function.

Here is my solution to the above scenario:

    global $wp_roles;
    $role = $wp_roles->roles['special']['name'];

Or in my case, what I was trying to achieve was:

    global $wp_roles;
    $u = get_userdata($user->ID);
    $role = array_shift($u->roles);
    $user->role = $wp_roles->roles[$role]['name'];

Hope this helps someone

Рауан Бағжан

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

if ($user_role == 'administrator') {
echo 'Administrator';
} elseif ($user_role == 'editor') {
echo 'Editor';
} elseif ($user_role == 'author') {
echo 'Author';
} elseif ($user_role == 'contributor') {
echo 'Contributor';
} elseif ($user_role == 'subscriber') {
echo 'Subscriber';
} else {
echo '<strong>' . $user_role . '</strong>';
}
?>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!