0

Just for clearance, if i add a custom post type with:

register_post_type( 'event',
    array(
        'labels' => array(…),
        'capability_type' => ['event','events'],
        'map_meta_cap' => true,
    )
);

what are the resulting capabilitites? (Singular/Plural)

'read_event'
'read_events'

I can not find a minimum variant that works. In all my projects i just double the capabilities, once singular, once plural... but i think that's shitty. Can anyone clear out when to user singular, and when to use plural?

2
  • 'capability_type' => ['event','events'], is a perfectly acceptable way to define capabilities for your post type. What's shitty about it? Commented Sep 5, 2019 at 6:33
  • not the definition is the problem, the question is: whats the resulting capabilitites? are all of them singular "read_event, delete_event, delete_others_event" or are they all plural "delete_events, read_events, delete_others_events"? It seems they are mixed, and i newer know which ones are the right ones. @JacobPeattie
    – Gooze
    Commented Sep 5, 2019 at 7:23

1 Answer 1

0

As is often the case, the answer lies in the documentation. In the documentation for register_post_type() you'll see:

  • 'capability_type'
    (string) The string to use to build the read, edit, and delete capabilities. May be passed as an array to allow for alternative plurals when using this argument as a base to construct the capabilities, e.g. array('story', 'stories'). Default 'post'.
  • 'capabilities'
    (array) Array of capabilities for this post type. $capability_type is used as a base to construct capabilities by default. See get_post_type_capabilities().

So capability_type uses the singular and plural strings provided to fill the capabilities argument, and if we follow that link to the documentation for get_post_type_capabilities() we can see the full list of capabilities (emphasis mine):

By default, seven keys are accepted as part of the capabilities array:

  • edit_post, read_post, and delete_post are meta capabilities, which are then generally mapped to corresponding primitive capabilities depending on the context, which would be the post being edited/read/deleted and the user or role being checked. Thus these capabilities would generally not be granted directly to users or roles.
  • edit_posts – Controls whether objects of this post type can be edited.
  • edit_others_posts – Controls whether objects of this type owned by other users can be edited. If the post type does not support an author, then this will behave like edit_posts.
  • publish_posts – Controls publishing objects of this post type.
  • read_private_posts – Controls whether private objects can be read.

These four primitive capabilities are checked in core in various locations. There are also seven other primitive capabilities which are not referenced directly in core, except in map_meta_cap(), which takes the three aforementioned meta capabilities and translates them into one or more primitive capabilities that must then be checked against the user or role, depending on the context.

  • read – Controls whether objects of this post type can be read.
  • delete_posts – Controls whether objects of this post type can be deleted.
  • delete_private_posts – Controls whether private objects can be deleted.
  • delete_published_posts – Controls whether published objects can be deleted.
  • delete_others_posts – Controls whether objects owned by other users can be can be deleted. If the post type does not support an author, then this will behave like delete_posts.
  • edit_private_posts – Controls whether private objects can be edited.
  • edit_published_posts – Controls whether published objects can be edited.

So by passing ['event','events'] all those capabilities with post will created but with event (eg. edit_event), and all the capabilities with posts will be created but with events (eg. edit_events). However, note that just passing 'event' to capability_type will have the exact same result. This is because if only a single value is passed WordPress will automatically add s for the plural. The only reason you'd need to pass the singular and plural versions separately is if the plural version is not just the same plus "s", such as story vs stories (if you only passed 'story' then the plural capabilities would be like edit_storys).

4
  • I've read the documentation, but can say, if i add_cap('edit_event') to my administrator role, the event-post-type is missing in admin menu. if i add_cap('edit_events') it shows up. Reading the Documentation i thought edit_event should map to edit_events?
    – Gooze
    Commented Sep 5, 2019 at 9:13
  • As described in the docs, the singular versions "are meta capabilities, which are then generally mapped to corresponding primitive capabilities depending on the context, which would be the post being edited/read/deleted and the user or role being checked. Thus these capabilities would generally not be granted directly to users or roles." Commented Sep 5, 2019 at 9:33
  • So you shouldn't be using add_cap('edit_event'). It doesn't make sense for a user to be able to "edit event". What event? A user can edit_events or edit_others_events. edit_event is mapped to either of those dynamically based on whether the user is the author of the event in question. Commented Sep 5, 2019 at 9:33
  • thanks for clearing that out.
    – Gooze
    Commented Sep 5, 2019 at 12:01

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.