WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/current user can

Description

Whether current user has a specific capability.

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

Usage

 <?php current_user_can$capability $object_id ); ?> 

Parameters

$capability
(string) (required) Role or capability.
Default: None
$object_id
(int) (optional) Recommended when checking meta capabilities such as the capabilities defined in the `map_meta_cap` function i.e 'edit_post', 'edit_others_posts', 'read_post' etc. If omitted you may receive an 'Undefined offset: 0' warning (this is because the `current_user_can` function eventually calls `map_meta_cap` which when checking against meta capabilities expects an array but is only supplied a single value)
Default: None

Returns

(boolean) 
current user has capability.

Notes

For additional information on the 'undefined index 0' warning please see this detailed response on the WordPress Stack Exchange.

current_user_can( $capability ) will aways return true if user is Super Admin, unless specifically denied - see inline source code on https://developer.wordpress.org/reference/classes/wp_user/has_cap/

Passing role names to current_user_can() is discouraged as this is not guaranteed to work correctly (see #22624).

Changelog

Since: 2.0.0

Source Code

current_user_can() is located in wp-includes/capabilities.php.

Related

Roles_and_Capabilities

See also index of Function Reference and index of Template Tags.