如何知道当前用户在WordPress中的作用?

时间:2010-08-17 03:22:53

标签: wordpress custom-post-type

当用户创建新帖子时,如何确定他当前的角色?

2 个答案:

答案 0 :(得分:9)

我假设您知道要使用的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.
}

答案 1 :(得分:0)

此代码可以帮助您

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