WordPress at a glance

author_can() WP 2.9.0

Checks whether the author of the supplied post has a specific capability.

✈ 1 time = 0.002667s = very slow | 50000 times = 1.86s = fast PHP 7.1.11, WP 4.9.4

No Hooks.

Return

true/false. Whether the post author has the given capability.

Usage

author_can( $post, $capability );
$post(int/WP_Post) (required)
Post ID or post object.
$capability(string) (required)
Capability name.

Examples

#1 Check whether the author of the current post ($post->ID) can publish posts:

if( author_can($post->ID, 'publish_posts') ){
	echo 'The author of the post can publish posts";
}

Code of author_can: wp-includes/capabilities.php VER 4.9.8

<?php
function author_can( $post, $capability ) {
	if ( !$post = get_post($post) )
		return false;

	$author = get_userdata( $post->post_author );

	if ( ! $author )
		return false;

	$args = array_slice( func_get_args(), 2 );
	$args = array_merge( array( $capability ), $args );

	return call_user_func_array( array( $author, 'has_cap' ), $args );
}

Related Functions

From tag: Roles capabilities

No comments
    Hello, !     Log In . Register