When you register your CPT, it’s possible to set up various capability schemes just for that CPT. You can then assign CPT capabilities to individual users, an existing role, or a newly created role. Adding roles and adding capabilities to those roles isn’t that well documented in the Codex. It’s possible to figure out what’s needed by studying the class definitions in wp-includes/capabilities.php, it’s not that complicated.
If you study the source code, you’ll see the properties are stored in the options table, so it’s not necessary to run the code every time WP loads. I am a big proponent of developing my own custom code and avoid installing a slew of plugins to meet my needs. Yet I have found it convenient to use one of the roles and capabilities plugins to easily manipulate these properties. I use Members, I like it, it works fine, but that is not to diminish any of the others available.
Thanks for coming back to me with that and I will take another look at the resource you have outlined. That was in fact the place I started however I could not quite get my head around it. Will take another look.
Thanks for your help.
Found the answer eventually thanks for looking at the way other plugins have done this, most notably the Whistles plugin by Justin Tadlock. Below is my solution.
When register the post type with register_post_type you can pass capability types like this (for a custom post type named bulletin):
'capability_type' => 'bulletin',
'map_meta_cap' => true,
'capabilities' => array(
// meta caps (don't assign these to roles)
'edit_post' => 'edit_bulletin',
'read_post' => 'read_bulletin',
'delete_post' => 'delete_bulletin',
// primitive/meta caps
'create_posts' => 'create_bulletins',
// primitive caps used outside of map_meta_cap()
'edit_posts' => 'edit_bulletins',
'edit_others_posts' => 'manage_bulletins',
'publish_posts' => 'manage_bulletins',
'read_private_posts' => 'read',
// primitive caps used inside of map_meta_cap()
'read' => 'read',
'delete_posts' => 'manage_bulletins',
'delete_private_posts' => 'manage_bulletins',
'delete_published_posts' => 'manage_bulletins',
'delete_others_posts' => 'manage_bulletins',
'edit_private_posts' => 'edit_bulletins',
'edit_published_posts' => 'edit_bulletins'
),
Then you need to assign the these capabilities to users on plugin activation (or another appropriate hook) to allow them to use the post type:
function mdw_plugin_activation() {
/* Get the administrator role. */
$mdw_role = get_role( 'administrator' );
/* If the administrator role exists, add required capabilities for the plugin. */
if ( !empty( $rhsstf_role ) ) {
$mdw_role->add_cap( 'manage_bulletins' );
$mdw_role->add_cap( 'create_bulletins' );
$mdw_role->add_cap( 'edit_bulletins' );
}
}
register_activation_hook( __FILE__, 'mdw_plugin_activation' );
i just want to say thank you for this post. It helped a lot.