What’s the difference between Role and Meta capabilities; When to use map_meta_cap() filter

difference between Role and Meta capabilities

That would be better to ask the compare Role with Capabilities, not just Meta capabilities, but as a simple answer:

A Role defines a set of tasks a user assigned the role is allowed to perform.

Capabilities are assigned to Roles

difference between Meta and Primitive capabilities?

Meta capabilities

edit_post, read_post, and delete_post are said to be Meta Capabilities because they depend on the context, for example the post being edited/read/deleted. That makes no sense if you say, some user has edit_post capability, because somebody will ask you: she/he can edit which post?

Primitive capabilities

These are capabilities like edit_posts which do not depend on any context, and are intrinsic to the user. If somebody has edit_posts capabality, she/he can edit posts.

when to use map_meta_cap() filter?

When you register new post type using register_post_type
, you have an option to use for capability_type which says, what are the capabilities, similar to? There you may just say post, meaning that you capabilities are similar to that of post post type.

Bu if you need more customization, you may say book or array( 'book', 'books' ) (the same), for example, to have the following capabilities:

capabilities' => array(
  'edit_post'          => 'edit_book', 
  'read_post'          => 'read_book', 
  'delete_post'        => 'delete_book', 
  'edit_posts'         => 'edit_books', 
  'edit_others_posts'  => 'edit_others_books', 
  'publish_posts'      => 'publish_books',       
  'read_private_posts' => 'read_private_books', 
  'create_posts'       => 'edit_books', 
)

When using capability_type as an array, e.g. array(‘story’, ‘stories’) the first element will be used for the singular capabilities and the second array element for the plural capabilities.

The map_meta_cap() function is called by WP_User->has_cap() to convert a meta capability to one or more primitive capabilities.

There is also a map_meta_cap parameter in register_post_type, which does the same thing,at the post_type registration.

Leave a Comment