Using WordPress ‘author_can()’ PHP function

The author_can() WordPress PHP function determines if the author of a provided post possesses a specific capability.

Usage

Here’s a basic usage of the function:

if (author_can($post, 'edit_posts')) {
    echo 'Author has the capability to edit posts.';
}

In this example, the function checks if the author of $post has the ‘edit_posts’ capability. If they do, it echoes a message confirming this.

Parameters

  • $post (int|WP_Post – Required): The ID or object of the post.
  • $capability (string – Required): The name of the capability to check.
  • $args (mixed – Optional): Additional parameters, typically starting with an object ID.

More information

See WordPress Developer Resources: author_can()

The author_can() function was implemented in WordPress 2.0.0. It checks meta capabilities, which are used by the map_meta_cap() function to map to primitive capabilities that a user or role has.

Examples

Check if the author can edit posts

This code snippet checks if the author of the post with ID 10 can edit posts.

$post = get_post(10); // Get post with ID 10
if (author_can($post, 'edit_posts')) {
    echo 'Author can edit posts.'; // Echoes this message if the author can edit posts
}

Check if the author can publish posts

This checks if the author of the post with ID 20 can publish posts.

$post = get_post(20); // Get post with ID 20
if (author_can($post, 'publish_posts')) {
    echo 'Author can publish posts.'; // Echoes this message if the author can publish posts
}

Check if the author can delete posts

This checks if the author of the post with ID 30 can delete posts.

$post = get_post(30); // Get post with ID 30
if (author_can($post, 'delete_posts')) {
    echo 'Author can delete posts.'; // Echoes this message if the author can delete posts
}

Check if the author can read posts

This checks if the author of the post with ID 40 can read posts.

$post = get_post(40); // Get post with ID 40
if (author_can($post, 'read')) {
    echo 'Author can read posts.'; // Echoes this message if the author can read posts
}

Check if the author can edit others’ posts

This checks if the author of the post with ID 50 can edit others’ posts.

$post = get_post(50); // Get post with ID 50
if (author_can($post, 'edit_others_posts')) {
    echo 'Author can edit others\' posts.'; // Echoes this message if the author can edit others' posts
}