Confusion with adding meta capabilities to a role after registering a Custom Post Type with corresponding ‘capability_type’ parameter

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:

Read More
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');
}

Related posts

Leave a Reply

2 comments

  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.

  2. Check below code.. for adding capabilities to administrator and editor…
    This code might work if you don’t want to use any plugin.

    function plugin_main_functions(){
    register_post_type('teachers', array(
        'labels' => array(
            'name'=>__('Teachers List', 'Teachers'),
            'add_new' => __('add new teacher', 'Teachers'),
            'add_new_item' => __('Add new teacher', 'Teachers')
            ),
        'public' => true,
        'supports'=> array('title', 'editor', 'thumbnail'),
        'menu_icon' => 'dashicons-groups',
        'capability_type' => array('teacher','teachers'),
        'map_meta_cap' => true
    
        ));
    
    }
    add_action('init','plugin_main_functions');
    
    register_activation_hook( __FILE__, function () {
    
        // Call function plugin_main_functions(),
        // The function where the custom post type is registered
        plugin_main_functions();
    
        $admin = get_role('administrator');
        $editor = get_role('editor');
    
        $admin->add_cap('edit_teacher');
        $admin->add_cap('edit_teachers');
        $admin->add_cap('read_teacher');
        $admin->add_cap('delete_teacher');
        $admin->add_cap('delete_teachers');
        $admin->add_cap('publish_teacher');
        $admin->add_cap('create_teachers');
    
        $editor->add_cap('edit_teacher');
        $editor->add_cap('edit_teachers');
        $editor->add_cap('read_teacher');
        $editor->add_cap('delete_teachers');
        $editor->add_cap('publish_teacher');
        $editor->add_cap('create_teachers');
    
        flush_rewrite_rules();
    
    } );
    
    register_deactivation_hook( __FILE__, function () {
    
        // Call function plugin_main_functions(),
        // The function where the custom post type is registered
        plugin_main_functions();
    
        $admin = get_role('administrator');
        $editor = get_role('editor');
    
        $admin->remove_cap('edit_teacher');
        $admin->remove_cap('edit_teachers');
        $admin->remove_cap('read_teacher');
        $admin->remove_cap('delete_teacher');
        $admin->remove_cap('delete_teachers');
        $admin->remove_cap('publish_teacher');
        $admin->remove_cap('create_teachers');
    
        $editor->remove_cap('edit_teacher');
        $editor->remove_cap('edit_teachers');
        $editor->remove_cap('read_teacher');
        $editor->remove_cap('delete_teachers');
        $editor->remove_cap('publish_teacher');
        $editor->remove_cap('create_teachers');
    
        flush_rewrite_rules();
    
    } );