9

I'm getting very confused about the following things:

  1. difference between Role and Meta capabilities,
  2. difference between Meta and Primitive capabilities,
  3. when to use map_meta_cap() filter.

There were lots of situations, where I add WP existing or custom capabilities to either WP existing or custom roles, expecting it to work out of the box and most of the times it doesn't.

I see that people mention about map_meta_cap() filter, but I can't find a good explanation what it actually does and when do I have to use it and when capabilities will work out of the box.

I hope that makes sense.

I would hugely appreciate explanations or links to resources that explain this well.

Thanks, Dasha

3

1 Answer 1

3

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.

1
  • ...If somebody has edit_posts capabality, she/he can edit posts... but which/whose posts?
    – T.Todua
    Apr 22, 2019 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.