I got quite confused with Custom Post Type (CPT) ‘capability_type’ parameter and adding generated capabilities to a role.
So I’ve got a CPT called ‘external_role’ registered as following:
add_action('init', 'external_roles_post_type_init');
function external_roles_post_type_init() {
$labels = array(
'name' => _x('External Roles', 'post type general name'),
'singular_name' => _x('external role', 'post type singular name'),
'add_new' => _x('Add New', 'external role'),
'add_new_item' => __('Add New External Role'),
'edit_item' => __('Edit External Role'),
'new_item' => __('New External Role'),
'view_item' => __('View External Role'),
'search_items' => __('Search External Roles'),
'not_found' => __('No job found'),
'not_found_in_trash' => __('No job found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'has_archive' => 'past-external-roles',
'rewrite' => array('slug' => 'external-roles','with_front' => false),
'query_var' => true,
'capability_type' => array('external_role', 'external_roles'),
'map_meta_cap' => true,
'hierarchical' => false,
'show_in_nav_menus' => false,
'menu_position' => 20,
'supports' => array(
'title',
'editor',
'author',
'custom-fields',
'revisions'
)
);
register_post_type('external_role',$args);
}
So looking at the capability_type
parameter (if I understand it correctly) will generate the following capabilities:
Meta capabilities:
- edit_external_role
- read_external_role
- delete_external_role
Primitive capabilities (that are generated based on meta capabilities):
- edit_external_role
- edit_others_external_role
- publish_external_role
- read_private_external_role
Ok, so now I would like to grand my capabilities to the admin
role, so admins can fully manage a new CPT.
I was thinking if I only grand meta capabilities
that would mean that all primitive capabilities
would be inclusively granted to the role as well? Is that the case?
Here is what I’ve got for adding capabilities to the admin role:
// Add caps for Administrator role
$role =& get_role('administrator');
// external roles capabilities
$role->add_cap('edit_external_role');
$role->add_cap('read_external_role');
$role->add_cap('delete_external_role');
However, this doesn’t do the trick.
What is the concise and correct way to add all the capabilities to a role to manage a CPT?
Many thanks,
Dasha
EDIT
I’m using the init
hook now in the functions.php
to add caps to the admin role:
add_action( 'init', 'my_custom_roles_capabilities', 0 );
function my_custom_roles_capabilities(){
// Add caps for Administrator role
$role =& get_role('administrator');
// external roles capabilities
$role->add_cap('edit_external_role');
$role->add_cap('read_external_role');
$role->add_cap('delete_external_role');
}
Have you tried defining the capabilities inside the external_roles_post_type_init() function?
After the
try add this
as another argument of the $args array
Then, before applying the capability to this or that role
you can try to install User Role Editor Plugin an look if the capabilities a really existent
If you see them you can even think about using just that plugin to give or revoke caps to roles.
I know it’s not like coding, but acually the ability to give and revoke with a click in admin panel it’s really handy.
I tell you this, because sometimes (quite often actually), I register custom post types with the very same method you use (plus the capabilities array that I’ve pasted above) but for some reason the capabilities are not really created.
Sometimes I solve just by moving registration function in upper position in functions.php file.
It seems like wichcraft to me also, but sometimes it works.
When I find myself in dead path like that the only solution I can find is using both User Role Editor and Map Cap plugins.
I do not like that, but it’s better than loosing mind.
I hope it helps you, even if it does not solve the “confusion” state that I’m sharing with you.
Check below code.. for adding capabilities to administrator and editor…
This code might work if you don’t want to use any plugin.