Custom Post Type Capabilities SingularPlural

Just for clearance, if i add a custom post type with:register_post_type( 'event',array('labels' =>

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?

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?

Share Improve this question asked Sep 4, 2019 at 10:39 GoozeGooze 171 silver badge7 bronze badges 2
  • 'capability_type' => ['event','events'], is a perfectly acceptable way to define capabilities for your post type. What's shitty about it? – Jacob Peattie 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
Add a comment  | 

1 Answer 1

Reset to default 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).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745183943a4615547.html

相关推荐

  • Custom Post Type Capabilities SingularPlural

    Just for clearance, if i add a custom post type with:register_post_type( 'event',array('labels' =>

    9小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信