函数文档

current_user_can()

💡 云策文档标注

概述

current_user_can() 函数用于检查当前用户是否具有指定的权限。它支持元权限检查,并可传递对象ID作为额外参数。超级管理员默认拥有所有权限,除非被明确拒绝。

关键要点

  • 检查当前用户的权限,返回布尔值
  • 支持元权限(如 edit_post),需通过 map_meta_cap() 映射到基础权限
  • 可传递额外参数(如对象ID)以检查特定对象的权限
  • 超级管理员默认返回 true,除非通过 map_meta_cap 过滤器显式拒绝
  • 避免直接检查角色,应检查权限以确保可靠性
  • 是 user_can() 的包装函数,内部调用 wp_get_current_user()

代码示例

current_user_can( 'edit_posts' );
current_user_can( 'edit_post', $post->ID );
current_user_can( 'edit_post_meta', $post->ID, $meta_key );

注意事项

  • 部分支持检查角色,但不推荐,可能导致不可靠结果
  • 相关函数:WP_User::has_cap()、map_meta_cap()
  • 参数:$capability(必需,权限名称),$args(可选,额外参数如对象ID)
  • 返回值:布尔值,表示用户是否拥有给定权限

📄 原文内容

Returns whether the current user has the specified capability.

Description

This function also accepts an ID of an object to check against if the capability is a meta capability. Meta capabilities such as edit_post and edit_user are capabilities used by the map_meta_cap() function to map to primitive capabilities that a user or role has, such as edit_posts and edit_others_posts.

Example usage:

current_user_can( 'edit_posts' );
current_user_can( 'edit_post', $post->ID );
current_user_can( 'edit_post_meta', $post->ID, $meta_key );

While checking against particular roles in place of a capability is supported in part, this practice is discouraged as it may produce unreliable results.

Note: Will always return true if the current user is a super admin, unless specifically denied.

See also

Parameters

$capabilitystringrequired
Capability name.
$argsmixedoptional
Optional further parameters, typically starting with an object ID.

Return

bool Whether the current user has the given capability. If $capability is a meta cap and $object_id is passed, whether the current user has the given meta capability for the given object.

Source

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

Changelog

Version Description
5.8.0 Converted to wrapper for the user_can() function.
5.3.0 Formalized the existing and already documented ...$args parameter by adding it to the function signature.
2.0.0 Introduced.

User Contributed Notes

  1. Skip to note 7 content

    If you want to check more than two roles, you can check if the roles of the current user is inside an array of roles, something like:

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

  2. Skip to note 10 content

    Note: Will always return true if the current user is a super admin, unless specifically denied.

    This note is a bit misleading. Explicitly denying the capability by using $user->add_cap( 'capability', false ) will not work. The only way to explicitly deny a capability for super admins is to use the map_meta_cap filter to return do_not_allow for checks for that capability.

  3. Skip to note 12 content

    The old Codex says current_user_can( $capability , $object_id ); but there’s no parameter $object_id in the head title on this page, even though it is listed in the Parameters section.