如何知道当前用户在WordPress中的作用?
-
29-09-2019 - |
题
当用户创建新帖子时,我该如何确定他的当前角色?
解决方案
我假设您知道要使用的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); ?>
不隶属于 StackOverflow