从投稿人名单中删除管理员栏“我的网站”链接

时间:2017-12-10 作者:cboy

我有一个milti网站,是为作者自定义帖子的贡献者建立的。我不能使用css修改adminbar,因为它删除了所有用户卷的链接。如何从contributor roll adminbar中删除我的网站、评论编号和新内容?

1 个回复
最合适的回答,由SO网友:Elex 整理而成

当您使用register_post_type 函数,在第二个参数中($args) 您可以更改一些参数。

有关更多信息,请参阅法典:https://codex.wordpress.org/Function_Reference/register_post_type#Parameters

良好的链接还包括:https://codex.wordpress.org/Roles_and_Capabilities#Capability_vs._Role_Table 获取有关默认功能和角色的更多信息

以下是“book”自定义帖子类型文档中的一个示例(将此添加到$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\', 
),
您还必须在参数中添加“map\\u meta\\u cap”:

\'map_meta_cap\' => true
有了它,您可以为自定义帖子类型使用映射设置“新”功能

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\');
为了回答在管理栏中隐藏一些按钮的问题,我将在body类中添加角色并添加一些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\');
}
实际上,我没有“干净”的想法来删除为自定义帖子类型发表评论的功能:/对不起,我的英语太差了!

结束

相关推荐

ADD_TIME_SUPPORT(‘admin-bar’)导致致命错误

我正在努力学习更多关于主题开发的知识,所以我创建了自己的主题,除了添加functions.php 并尝试用一些简单的方法进行更新,如:<?php add_theme_support(\'admin-bar\', array(\'menus\')); ?> 我明白了Server 500 ERROR 我无法访问Wordpress的任何部分,甚至连仪表板都无法访问。但一旦我删除functions.php 和刷新页面我的Wordpress又回来了,工作顺利。有什么神秘的fu