programing

Wordpress 역할 표시 이름

instargram 2023. 4. 2. 09:43
반응형

Wordpress 역할 표시 이름

Wordpress 역할 표시명을 얻을 수 있는 깔끔한 방법을 찾고 있습니다.

역할을 추가할 때(예:add_role( 'special', 'A Special Role');표시명을 설정할 수 있습니다.

역할을 얻었을 때(예:get_role( 'special' );) 반환되는 역할 오브젝트에는 이름 및 기능 오브젝트만 있습니다.

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

표시명도 없습니다.표시명은 WP Admin 백엔드(users.php 및 user-edit.php) 전체에서 사용되고 있습니다만, 래빗 워렌입니다만, 그것을 반환하는 함수를 찾을 수 없었습니다.에서 쉽게 확인할 수 있습니다.wp_user_roles노를 젓다wp-options테이블 - 제가 거기에 갈 필요는 없겠죠?

영어 이외의 WordPress 사이트를 실행하고 있으며 모든 WordPress 관리 페이지에 표시되는 적절한(번역된) 역할 이름을 표시하려면 다음과 같이 하십시오.

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

어디에$role는 역할 이름입니다(즉,'special'를 참조해 주세요).

주의: translate_user_role은 문서화되어 있지 않은 WordPress 핵심 함수입니다.

위의 시나리오에 대한 솔루션을 다음에 나타냅니다.

    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'];

이게 도움이 됐으면 좋겠는데

<?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>';
}
?>

언급URL : https://stackoverflow.com/questions/25059638/wordpress-role-display-name

반응형