Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何知道当前用户在WordPress中的角色?_Wordpress_Custom Post Type - Fatal编程技术网

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

如何知道当前用户在WordPress中的角色?,wordpress,custom-post-type,Wordpress,Custom Post Type,当用户创建新帖子时,我如何确定他当前的角色?我假设您知道要使用Wordpress的哪些钩子。因此跳过这一部分,很容易获得用户的当前角色 $current_user = wp_get_current_user(); if ( !($current_user instanceof WP_User) ) return; $roles = $current_user->roles; //$roles is an array 现在,您可以迭代该数组以查看用户是否具有特定角色 或者,您可以使

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

我假设您知道要使用Wordpress的哪些钩子。因此跳过这一部分,很容易获得用户的当前角色

$current_user = wp_get_current_user();
if ( !($current_user instanceof WP_User) )
   return;
$roles = $current_user->roles;  //$roles is an array
现在,您可以迭代该数组以查看用户是否具有特定角色

或者,您可以使用
当前用户\u can
查找特定功能,如果您只想检查用户是否具有特定权限,而不是他们是否在角色中。例如:

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

此代码将帮助您

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


太棒了!这正是我要找的!:)谢谢顺便问一下,我必须使用哪个钩子来编写这段代码?请注意,现在的WP codex声明:“不要将角色名称传递给当前的用户,因为这不能保证正常工作”。它只是在最后遗漏了这一点:如果(在数组中('super_admin',$roles)){echo'foobar',}你能解释一下它将如何解决这个问题吗?