programing

WordPress에서 현재 사용자의 역할을 아는 방법

starjava 2023. 2. 26. 08:39
반응형

WordPress에서 현재 사용자의 역할을 아는 방법

사용자가 새 게시물을 작성할 때 현재 역할을 확인하려면 어떻게 해야 합니까?

워드프레스의 후크가 뭔지 알고 있을 겁니다이 부분을 건너뛰면 사용자의 현재 역할을 쉽게 파악할 수 있습니다.

$current_user = wp_get_current_user();
if ( !($current_user instanceof WP_User) )
   return;
$roles = $current_user->roles;  //$roles is an array

이제 해당 어레이를 반복하여 사용자에게 특정 역할이 있는지 확인할 수 있습니다.

또는 를 사용할 수 있습니다.current_user_can사용자가 특정 권한을 가지고 있는지 여부와 역할에 속해 있는지 여부를 확인하는 경우.예를 들어 다음과 같습니다.

if (current_user_can('delete_posts')) {
  //display the delete posts button.
}

이 코드가 도움이 됩니다.

<?php  echo array_shift(wp_get_current_user()->roles); ?>

언급URL : https://stackoverflow.com/questions/3499104/how-to-know-the-role-of-current-user-in-wordpress

반응형