Ask your WordPress questions! Pay money and get answers fast! (more info)

Need to add capabilities and role to custom post type WordPress

  • SOLVED

I have the following custom post type in my functions.php file:

<code

add_action( 'init', 'register_cpt_team_member' );

function register_cpt_team_member() {

$labels = array(
'name' => _x( 'Team Members', 'team_member' ),
'singular_name' => _x( 'Team Member', 'team_member' ),
'add_new' => _x( 'Add New Team Member', 'team_member' ),
'add_new_item' => _x( 'Add New Team Member', 'team_member' ),
'edit_item' => _x( 'Edit Team Member', 'team_member' ),
'new_item' => _x( 'New Team Member', 'team_member' ),
'view_item' => _x( 'View Team Member', 'team_member' ),
'search_items' => _x( 'Search Team Members', 'team_member' ),
'not_found' => _x( 'No team members found', 'team_member' ),
'not_found_in_trash' => _x( 'No team members found in Trash', 'team_member' ),
'parent_item_colon' => _x( 'Parent Team Member:', 'team_member' ),
'menu_name' => _x( 'Team Members', 'team_member' ),
);

$args = array(
'labels' => $labels,
'hierarchical' => false,
'description' => 'Team members',
'supports' => array( 'title', 'author' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => array('slug' => ''),
);

$capabilities = array(
'edit_post' => 'edit_team_member',
'read_post' => 'read_team_member',
'delete_post' => 'delete_team_member',
'edit_posts' => 'edit_team_member',
'edit_others_posts' => 'edit_others_team_member',
'publish_posts' => 'publish_team_member',
'read_private_posts' => 'read_private_team_member',
);

register_post_type( 'team-member', $args );
}

</code>

The above CPT works fine, but I need to add a role called Team Member to it, and give the role read, publish and edit rights (with admin having everything).

Can someone show me how to do this? I'm using the Members plugin from Justin Tadlock, and I don't see any capabilities for this CPT.

Thanks!

Answers (3)

2015-07-10

Darlene Grace Arcenal answers:

You need to use add_cap

function add_theme_caps() {
// gets the administrator role
$admins = get_role( 'administrator' );

$admins->add_cap( 'edit_gallery' );
$admins->add_cap( 'edit_galleries' );
$admins->add_cap( 'edit_other_galleries' );
$admins->add_cap( 'publish_galleries' );
$admins->add_cap( 'read_gallery' );
$admins->add_cap( 'read_private_galleries' );
$admins->add_cap( 'delete_gallery' );
}
add_action( 'admin_init', 'add_theme_caps');

The above is for the administrator role, replace administrator with your role name.


Kyler Boudreau comments:

Where exactly do I place that? Thanks!


Darlene Grace Arcenal comments:

Place that on your functions.php below the register_cpt_team_member function


Darlene Grace Arcenal comments:

also Add this map_meta_cap => true below the capabilities so

$capabilities = array(
'edit_post' => 'edit_team_member',
'read_post' => 'read_team_member',
'delete_post' => 'delete_team_member',
'edit_posts' => 'edit_team_member',
'edit_others_posts' => 'edit_others_team_member',
'publish_posts' => 'publish_team_member',
'read_private_posts' => 'read_private_team_member',
),
'map_meta_cap' => true


Kyler Boudreau comments:

Darlene,

Sorry to be a pain, but when I add that code in and change the administrator to my role, the site white screens.

Can you add it in exactly where it would go in my code above? I'm a front end designer, and a PHP lightweight. Very lightweight.


Darlene Grace Arcenal comments:

Sorry it's is incorrect add this to here

$args = array(
'labels' => $labels,
'hierarchical' => false,
'description' => 'Team members',
'supports' => array( 'title', 'author' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => array('slug' => ''),
<strong>'map_meta_cap' => true</strong>
);


Darlene Grace Arcenal comments:

Try to replace all your code with this please


add_action( 'init', 'register_cpt_team_member' );

function register_cpt_team_member() {

$labels = array(
'name' => _x( 'Team Members', 'team_member' ),
'singular_name' => _x( 'Team Member', 'team_member' ),
'add_new' => _x( 'Add New Team Member', 'team_member' ),
'add_new_item' => _x( 'Add New Team Member', 'team_member' ),
'edit_item' => _x( 'Edit Team Member', 'team_member' ),
'new_item' => _x( 'New Team Member', 'team_member' ),
'view_item' => _x( 'View Team Member', 'team_member' ),
'search_items' => _x( 'Search Team Members', 'team_member' ),
'not_found' => _x( 'No team members found', 'team_member' ),
'not_found_in_trash' => _x( 'No team members found in Trash', 'team_member' ),
'parent_item_colon' => _x( 'Parent Team Member:', 'team_member' ),
'menu_name' => _x( 'Team Members', 'team_member' ),

);

$args = array(
'labels' => $labels,
'hierarchical' => false,
'description' => 'Team members',
'supports' => array( 'title', 'author' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => array('slug' => ''),
'capabilities' => array(
'edit_post' => 'edit_team_member',
'read_post' => 'read_team_member',
'delete_post' => 'delete_team_member',
'edit_posts' => 'edit_team_member',
'edit_others_posts' => 'edit_others_team_member',
'publish_posts' => 'publish_team_member',
'read_private_posts' => 'read_private_team_member',
),
'map_meta_cap' => true
);

register_post_type( 'team-member', $args );
}

add_role( 'team_member', 'Team Member', array( 'edit_posts' => true, ) );
function add_team_caps_to_admin() {
$caps = array(
'edit_team_member',
'read_team_member',
'delete_team_member',
'edit_others_team_member',
'publish_team_member',
'read_private_team_member',
);
$roles = array(
get_role( 'administrator' ),
get_role( 'team_member' ),
);
foreach ($roles as $role) {
foreach ($caps as $cap) {
$role->add_cap( $cap );
}
}
}
add_action( 'after_setup_theme', 'add_team_caps_to_admin' );

2015-07-10

timDesain Nanang answers:

Hi Kyler,
you can try the following code (put in the theme's function.php).

since add role and capabilities just need to <strong>be called once</strong>, not each page load,
then the code was using <strong>after_switch_theme</strong> hook, you need to:
1. switch theme to another one
2. reactivate the theme before



function wpq_after_switch_theme() {
register_cpt_team_member();
wpq_activate_roles();
flush_rewrite_rules();
}
add_action( 'after_switch_theme', 'wpq_after_switch_theme' );


add_action( 'init', 'register_cpt_team_member' );
function register_cpt_team_member() {

$labels = array(
'name' => _x( 'Team Members', 'team_member' ),
'singular_name' => _x( 'Team Member', 'team_member' ),
'add_new' => _x( 'Add New Team Member', 'team_member' ),
'add_new_item' => _x( 'Add New Team Member', 'team_member' ),
'edit_item' => _x( 'Edit Team Member', 'team_member' ),
'new_item' => _x( 'New Team Member', 'team_member' ),
'view_item' => _x( 'View Team Member', 'team_member' ),
'search_items' => _x( 'Search Team Members', 'team_member' ),
'not_found' => _x( 'No team members found', 'team_member' ),
'not_found_in_trash' => _x( 'No team members found in Trash', 'team_member' ),
'parent_item_colon' => _x( 'Parent Team Member:', 'team_member' ),
'menu_name' => _x( 'Team Members', 'team_member' ),
);

$args = array(
'labels' => $labels,
'hierarchical' => false,
'description' => 'Team members',
'supports' => array( 'title', 'author' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => array('slug' => ''),

//custom cpt capabilites
'capability_type' => 'team_member',
'map_meta_cap' => true,
);


register_post_type( 'team-member', $args );
}


function wpq_activate_roles() {
if(class_exists('WP_Roles')){
if(!isset($wp_roles)) $wp_roles = new WP_Roles();
if(is_object($wp_roles)) {

//team_member
$team_caps = $GLOBALS['wp_post_types']['team-member']->cap ;
foreach($team_caps as $key=>$value){
$wp_roles->add_cap('administrator', $value);
//$wp_roles->add_cap('editor', $value);
}

$wp_roles->add_role('team_member', 'Team Member', array('read' => true));
//$wp_roles->add_cap('team_member', 'upload_files');
$wp_roles->add_cap('team_member', 'read_team_member');
$wp_roles->add_cap('team_member', 'edit_team_member');
$wp_roles->add_cap('team_member', 'edit_team_members');
$wp_roles->add_cap('team_member', 'publish_team_members');
$wp_roles->add_cap('team_member', 'edit_published_team_members');

}
}
}


timDesain Nanang comments:

you shouldn't use dashes in the custom post type.
<blockquote>if the post type contains dashes you will not be able to add columns to the custom post type's admin page (using the 'manage_<Custom Post Type Name>_posts_columns' action).</blockquote>
ref: [[LINK href="https://codex.wordpress.org/Function_Reference/register_post_type#Reserved_Post_Types"]]https://codex.wordpress.org/Function_Reference/register_post_type#Reserved_Post_Types[[/LINK]]


Kyler Boudreau comments:

Tim,

This worked beautifully - thanks for the note on theme activation.

2015-07-10

Ian Lincicome answers:

Kyler,
Nice talking to you again lol. I saw a post on how to do capabilities for custom post types, maybe this will help:
http://chrisplaneta.com/theme-dev/create-custom-post-types-custom-capabilities/
Good luck,
Ian