PHP Wordpress:检查当前用户的角色

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP Wordpress:检查当前用户的角色相关的知识,希望对你有一定的参考价值。

/**
* Checks if a particular user has a role. 
* Returns true if a match was found.
*
* http://codex.wordpress.org/Roles_and_Capabilities#Subscriber
* http://docs.appthemes.com/tutorials/wordpress-check-user-role-function/
* http://wordpress.org/support/topic/how-to-get-the-current-logged-in-users-role
* 

* @param string $role Role name.
* @param int $user_id (Optional) The ID of a user. Defaults to the current user.
* @return bool
*/
function appthemes_check_user_role( $role, $user_id = null ) {
 
	if ( is_numeric( $user_id ) )
		$user = get_userdata( $user_id );
	else
		$user = wp_get_current_user();
 
	if ( empty( $user ) )
		return false;
 
	return in_array( $role, (array) $user->roles );
}
 
 
// example use for the current user
if ( appthemes_check_user_role( 'subscriber' ) ){
	show_admin_bar(false);
} else {
	_e( "Sorry man, no luck.", 'appthemes' );
}

以上是关于PHP Wordpress:检查当前用户的角色的主要内容,如果未能解决你的问题,请参考以下文章

Wordpress - 通过检查用户状态从 bbPress 外部更改论坛角色

检查当前用户的角色

用户角色 - 添加角色功能 php 片段 - Wordpress

Wordpress - 更改 function.php 中的插件选项

如何从 PHP 代码自定义 Wordpress DashBoard 中的用户角色?

php 列出所有WordPress用户角色