0

I have a milti-site that is set up for contributors to author custom posts. I can't use css to modify the adminbar because it remover the links for all user rolls. How can I remove My Sites, Comment Number and New from the contributor roll adminbar?

  • Quick question before answer, do you want to remove the right to post see comments and create a post to ? Because just hiding buttons on admin bar can be only front restrictive but they continue to have access with URL. – Elex Dec 10 '17 at 13:24
  • I want to remove the right to post, see comments and create a post too. I want to remove everything on the admin bar for contributors except for the custom post name, which gets them back and forward to the dashboard and the howdy user name – cboy Dec 11 '17 at 20:27
0

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 !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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