I'm writing a plugin which creates a custom post_type. I'd also like the plugin to create a custom role which can only add/edit/delete the new post_type. I've tried several plugins (Role Scoper, Advanced Access manager) and they allow me to redefine or create new roles, but they don't allow me to assign capabilities specific to the new post_type. For example, I want to allow the ability to add/edit my new post_type but NOT normal posts/pages.
From what I've read, I can add new roles with the add_role() function. One of the parameters of this function is an array of "capabilities" which appear to be defined here. I think what I need is to be able to add my capabilities that are specific to MY post_type. Is this possible?
Nowadays (WP 3.5+), it's much easier. Just set the
map_meta_cap
argument toTRUE
and choose astring
(typically the post type name) for thecapability_type
argument when registering the post type.A simple
var_dump( $GLOBALS['wp_post_types']['new_custom_post_type'] ) );
will show you something like the following.The more intended array part are the seven other primitive capabilities that are not checked by core, but mapped by
map_meta_caps()
during post type registration.Capabilities for Custom Post Types
The function
register_post_type()
takes a$capabilities
array as one of its (optional) arguments.It could look like so:
where "ypt" stands for "your post type".
Thereafter you could add a new role to your WordPress that has these exact capabilities (and possibly some more of the standard WordPress capabilities):
The latter can be done using plugins though, check out the Members plugin by Justin Tadlock, for instance.
Thorough Example
To give you a more concrete example: