Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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');
}
share|improve this question
    
As far as I can tell, this generally looks good. Where are you running the code to add the capabilities? What hook is being used? It might be useful at different places in your script to do a 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. –  tollmanz Sep 13 '11 at 15:50
    
@tollmanz I'm adding capabilities in my functions.php file...hm I'm not using any hook, I just have the code of adding caps as it appears above. The edit/read/delete_external_role meta caps are granted, but only these ones, I thought that they would expand to include derived primitive caps, like edit_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. –  dashaluna Sep 14 '11 at 9:30
    
I'm still very confused, found a tutorial by Justin Tadlock, called "Meta capabilities for custom post types" justintadlock.com/archives/2010/07/10/… If I understand that correctly after adding capabilities to a role I would need to map them using map_meta_cap filter. I did follow Justin's tutorial, but after adding the map_meta_cap filter I still can't see anything under "External Roles". I'm very confused, would really appreciate any help! Thanks –  dashaluna Sep 14 '11 at 14:26

1 Answer 1

Have you tried defining the capabilities inside the external_roles_post_type_init() function?

After the

'capability_type' => array('external_role', 'external_roles'),

try add this

'capabilities' => array(
    'publish_posts' => 'publish_external_roles',
    'edit_posts' => 'edit_external_roles',
    'edit_others_posts' => 'edit_others_external_roles',
    'delete_posts' => 'delete_external_roles',
    'delete_others_posts' => 'delete_others_external_roles',
    'read_private_posts' => 'read_private_external_roles',
    'edit_post' => 'edit_external_role',
    'delete_post' => 'delete_external_role',
    'read_post' => 'read_external_role',
        ),

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.