Wordpress角色显示名称

时间:2014-07-31 13:06:12

标签: wordpress roles

我正在寻找一种巧妙的方法来获取Wordpress角色显示名称。

添加角色(例如add_role( 'special', 'A Special Role');)时,您可以设置显示名称。

获得角色(例如get_role( 'special' );)时,返回的角色对象只具有名称和功能对象。

WP_Role Object(
[name] => special
[capabilities] => Array() )

显示名称。显示名称在WP Admin后端(users.php和user-edit.php)中使用,但它是一只兔子warren ...而且我找不到会返回它的函数。你可以在wp_user_roles表的wp-options行中看到它 - 当然我不需要去那里?

3 个答案:

答案 0 :(得分:9)

如果您正在运行一个非英语WordPress网站,并希望显示正确的(已翻译的)角色名称,就像它出现在所有WordPress管理页面中一样,以下是如何执行此操作:

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

其中$role是角色名称(即原始问题中的'special')。

注意:translate_user_role是WordPress核心未记录的功能。

答案 1 :(得分:0)

以下是我对上述方案的解决方案:

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

或者就我而言,我想要实现的目标是:

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

希望这有助于某人

答案 2 :(得分:0)

<?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>';
}
?>
相关问题