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');
}
var_dump
of$wp_roles
:global $wp_roles; var_dump(
$wp_roles);
. The only thing I can think of here is that perhaps there's an ordering issue at play...like the roles are being granted before the CPT is registered; however, with that said, I'm not sure that that would actually cause a problem. I may take a closer look at this later today.functions.php
file...hm I'm not using any hook, I just have the code of adding caps as it appears above. Theedit/read/delete_external_role
meta caps are granted, but only these ones, I thought that they would expand to include derived primitive caps, likeedit_others_external_roles
, but they don't. Do I need to specify every capability separately? Basically, I was trying to grand all the capabilities in a few lines of code. Hope that makes sense.map_meta_cap
filter. I did follow Justin's tutorial, but after adding themap_meta_cap
filter I still can't see anything under "External Roles". I'm very confused, would really appreciate any help! Thanks