When you use register_post_type
function, in the second parameter ($args
) you can change some parameters.
See the codex for more informations : https://codex.wordpress.org/Function_Reference/register_post_type#Parameters
A good link is also : https://codex.wordpress.org/Roles_and_Capabilities#Capability_vs._Role_Table to get more information about default capabilites and roles
Here is an example from the docs for “book” custom post type (add this in your $args
)
'capabilities' => array(
'edit_post' => 'edit_book',
'read_post' => 'read_book',
'delete_post' => 'delete_book',
'edit_posts' => 'edit_books',
'edit_others_posts' => 'edit_others_books',
'publish_posts' => 'publish_books',
'read_private_posts' => 'read_private_books',
'create_posts' => 'edit_books',
),
You must also add a ‘map_meta_cap’ in the args :
'map_meta_cap' => true
With this you can set “new” capabilities with the map for your custom post type
function wpse_288266_add_theme_caps() {
// Add cap for the admins
$admins = get_role('administrator');
$admins->add_cap('edit_book');
$admins->add_cap('edit_books');
$admins->add_cap('edit_other_books');
$admins->add_cap('publish_books');
$admins->add_cap('read_book');
$admins->add_cap('read_private_books');
$admins->add_cap('delete_book');
$contribs = get_role('contributor');
$contribs->add_cap('edit_book');
$contribs->add_cap('edit_books');
$contribs->add_cap('edit_other_books');
$contribs->add_cap('publish_books');
$contribs->add_cap('read_book');
$contribs->add_cap('read_private_books');
$contribs->add_cap('delete_book');
// Here you remove the rights for other post type without the capabilities mapping
$contribs->remove_cap('edit_post');
$contribs->remove_cap('edit_posts');
$contribs->remove_cap('edit_other_posts');
$contribs->remove_cap('publish_posts');
$contribs->remove_cap('delete_post');
/* You'll probably want to let them read the posts
$contribs->remove_cap('read_post');
$contribs->remove_cap('read_private_posts');
*/
}
add_action('admin_init', 'wpse_288266_add_theme_caps');
To answer about hide some buttons in the admin bar I’ll add the role in the body class and add some CSS
function wpse_288266_add_role_body_class($classes) {
// Get the current user (the action happend when logged in)
$current_user = new WP_User(get_current_user_id());
$role = array_shift($current_user->roles);
if(is_admin())
{
$classes .= 'user-role-'. $role;
}
else
{
$classes[] = 'user-role-'. $role;
}
return $classes;
}
function wpse_288266_add_role_style() {
?>
<style type="text/css" media="screen">
body.user-role-contibutor #wpadminbar #wp-admin-bar-comments {
display: none;
}
body.user-role-contibutor.single-YOUR_POST_TYPE_KEY #wpadminbar #wp-admin-bar-comments {
display: list-item;
}
</style>
<?php
}
// We apply our code only for logged-in users
if(is_user_logged_in())
{
add_filter('body_class','wpse_288266_add_role_body_class');
add_filter('admin_body_class','wpse_288266_add_role_body_class');
add_action('wp_footer', 'wpse_288266_add_role_style'); // You can do better directly in your real stylesheet
add_action('admin_footer', 'wpse_288266_add_role_style');
}
I actually have no “clean” idea to remove the capability to post a comment for a custom post type :/
Sorry about my English !