wppaste
WordPress

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

Since
2.0.0, 5.3.0, 5.8.0
Source
wp-includes/capabilities.php:910

Test whether the current user has a capability with current_user_can(), from broad caps like manage_options to meta caps like edit_post with a post ID. Returns a boolean; super admins pass almost every check, and passing role names instead of capabilities gives unreliable results.

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.

Compatibility

WordPress
since 5.8.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$capabilitystring
Capability name.
$argsmixed
Optional further parameters, typically starting with an object ID.

Return value

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.

Code examples

Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.

Check a capability against a specific post

Meta capabilities like edit_post take the post ID and are mapped to the real capability for that post.

wp_set_current_user( 1 );

echo 'as administrator:\n';
echo '  manage_options: ', var_export( current_user_can( 'manage_options' ), true ), "\n";
echo '  edit_post 1:    ', var_export( current_user_can( 'edit_post', 1 ), true ), "\n\n";

wp_set_current_user( 0 );

echo "as a logged-out visitor:\n";
echo '  manage_options: ', var_export( current_user_can( 'manage_options' ), true ), "\n";
echo '  edit_post 1:    ', var_export( current_user_can( 'edit_post', 1 ), true );

Never check the role instead; capabilities are what plugins and roles actually modify.

Gate a form handler by capability

Check the capability at the top of any state-changing handler, then verify the nonce; the two checks answer different questions.

add_action( 'admin_post_myplugin_save', 'myplugin_handle_save' );

function myplugin_handle_save() {
	if ( ! current_user_can( 'manage_options' ) ) {
		wp_die( esc_html__( 'You are not allowed to do that.', 'myplugin' ) );
	}
	check_admin_referer( 'myplugin_save' );

	// ...validate and persist the submitted settings...

	wp_safe_redirect( admin_url( 'options-general.php?page=myplugin&saved=1' ) );
	exit;
}

A capability check alone does not prevent CSRF: the user may be capable but the request forged. Always pair current_user_can() with a nonce check before writing anything.

Performance profile

How much work a call to current_user_can() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
11

Executed per call on PHP 8.5. The body compiles to 11.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
50

50 places in core call this, so the cost is paid more often than your own code shows.

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost current_user_can() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always11wp_get_current_user(), user_can()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev11110
8.511110
8.411110
8.311110
8.211110
8.1111101 more instruction than PHP 7.4
7.410100

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 2

Used by · 50

Show all 50

Source code

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

Changelog

Introduced in 2.0.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

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

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-includes/capabilities.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.