WordPress函数:current_user_can 检查当前用户的权限

江河/ 2023年08月14日/ WordPress/ 浏览 1278

函数原型:


current_user_can( string $capability, mixed $args ): bool


返回当前用户是否具有指定的功能。


此函数还接受对象的ID,以检查该功能是否为元功能。元功能(如edit_post和edit_user)是map_Meta_cap()函数用来映射到用户或角色所具有的基元功能的功能,如edit_posts和edit_others_posts。


参数说明:


$capability,功能名称。


$args,可选的其他参数,通常以对象ID开头。


函数源码:


function current_user_can( $capability, ...$args ) {
	return user_can( wp_get_current_user(), $capability, ...$args );
}


使用举例:


$user = wp_get_current_user();
$allowed_roles = array( 'editor', 'administrator', 'author' );
if ( array_intersect( $allowed_roles, $user->roles ) ) {
   // Stuff here for allowed roles
}


发表评论

暂无评论,抢个沙发...

客服 工单