The map_meta_cap() WordPress PHP function maps a capability to the primitive capabilities required for a given user to satisfy the capability being checked.
Usage
map_meta_cap( 'edit_posts', $user->ID ); map_meta_cap( 'edit_post', $user->ID, $post->ID ); map_meta_cap( 'edit_post_meta', $user->ID, $post->ID, $meta_key );
Parameters
$cap (string)
– Required. Capability being checked.$user_id (int)
– Required. User ID.$args (mixed)
– Optional. Further parameters, typically starting with an object ID.
More information
See WordPress Developer Resources: map_meta_cap
Examples
Check if the user can edit posts
$required_caps = map_meta_cap( 'edit_posts', $user->ID ); foreach ( $required_caps as $cap ) { if ( ! user_can( $user->ID, $cap ) ) { echo "User cannot edit posts."; break; } }
Check if the user can edit a specific post
$required_caps = map_meta_cap( 'edit_post', $user->ID, $post->ID ); foreach ( $required_caps as $cap ) { if ( ! user_can( $user->ID, $cap ) ) { echo "User cannot edit this post."; break; } }
Check if the user can edit a specific post’s meta
$required_caps = map_meta_cap( 'edit_post_meta', $user->ID, $post->ID, $meta_key ); foreach ( $required_caps as $cap ) { if ( ! user_can( $user->ID, $cap ) ) { echo "User cannot edit this post's meta."; break; } }
Check if the user can delete a specific post
$required_caps = map_meta_cap( 'delete_post', $user->ID, $post->ID ); foreach ( $required_caps as $cap ) { if ( ! user_can( $user->ID, $cap ) ) { echo "User cannot delete this post."; break; } }
Check if the user can publish a specific post
$required_caps = map_meta_cap( 'publish_post', $user->ID, $post->ID ); foreach ( $required_caps as $cap ) { if ( ! user_can( $user->ID, $cap ) ) { echo "User cannot publish this post."; break; } }