我如何才能允许特定角色的自定义分类?

时间:2015-11-07 作者:DᴀʀᴛʜVᴀᴅᴇʀ

我试图只显示editoradministrator 但我还没有找到解决办法。我能找到的最接近的东西是How do I remove a taxonomy from Wordpress? 如此这般。只允许对editoradministrator?

// remove from everyone that isn\'t admin or editor
function taxonomy_for_admin_and_editor_only() {
   if ( !current_user_can( \'administrator\' ) || !current_user_can( \'editor\' ) ) {
       register_taxonomy( \'foobar\', array() );
   }
}
add_action( \'init\' , \'taxonomy_for_admin_and_editor_only\' );
我试过了add_action( \'admin\' , \'taxonomy_for_admin_and_editor_only\' ); 也没有运气。

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

注册分类法时,可以传递一个名为capabilities. 只需传递只有管理员和编辑才具有的能力。

$args = array(
    \'capabilities\' => array( \'manage_options\', \'edit_posts\' )
);
register_taxonomy( \'foobar\', \'post\', $args ); 

https://codex.wordpress.org/Roles_and_Capabilities#Editor

SO网友:DᴀʀᴛʜVᴀᴅᴇʀ

我以前从未扮演过角色,所以这是一次有趣的学习经历。我离开这里是因为我花了很多时间研究这个领域,这可能会帮助下一个人。我知道我的问题是关于分类法的,但我对这个问题感到困惑,所以我想从CPT级别进行测试。正确的挂钩是admin_init 然而,它仍在从administratoreditor 我不想那样。

在我遇到答案之前”Possible to hide Custom Post Type UI/Menu from specific User Roles?“但当我使用以下函数时:

// hide from Contributors
function no_see_from_contributors() {
    if ( current_user_can( \'contributor\' ) ) :
        remove_menu_page( \'edit.php\' ); // Posts
        remove_menu_page( \'tools.php\' ); // Tools
        remove_menu_page( \'edit-comments.php\' ); // Comments
    endif;
}
add_action( \'admin_menu\', \'no_see_from_contributors\' );
创建一个测试参与者帐户,即使它在菜单中不可见,参与者仍然可以通过添加edit.php, tools.phpedit-comments.php 在URL中。我将测试一个解决方案,以防止出现这种情况,并在找到解决方案时使用该解决方案进行编辑。

之后的进一步研究导致了“Restrict custom post type to only site administrator role“和”Remove Custom Post Type menu for non-administrator users“这本书读得很好。要了解有关功能的更多信息,您可以访问Codex: register post type 并向下滚动至capability_type.

您可以参考法典AdministratorEditor 在阵列中使用哪些角色。

最终分类法:

function something_taxonomy() {
    $labels = array(
        \'name\'                       => _x( \'Taxonomies\', \'Taxonomy General Name\', \'theme\' ),
        \'singular_name\'              => _x( \'Taxonomy\', \'Taxonomy Singular Name\', \'theme\' ),
        \'menu_name\'                  => __( \'Taxonomy\', \'theme\' ),
        \'all_items\'                  => __( \'All Items\', \'theme\' ),
        \'parent_item\'                => __( \'Parent Item\', \'theme\' ),
        \'parent_item_colon\'          => __( \'Parent Item:\', \'theme\' ),
        \'new_item_name\'              => __( \'New Item Name\', \'theme\' ),
        \'add_new_item\'               => __( \'Add New Item\', \'theme\' ),
        \'edit_item\'                  => __( \'Edit Item\', \'theme\' ),
        \'update_item\'                => __( \'Update Item\', \'theme\' ),
        \'view_item\'                  => __( \'View Item\', \'theme\' ),
        \'separate_items_with_commas\' => __( \'Separate items with commas\', \'theme\' ),
        \'add_or_remove_items\'        => __( \'Add or remove items\', \'theme\' ),
        \'choose_from_most_used\'      => __( \'Choose from the most used\', \'theme\' ),
        \'popular_items\'              => __( \'Popular Items\', \'theme\' ),
        \'search_items\'               => __( \'Search Items\', \'theme\' ),
        \'not_found\'                  => __( \'Not Found\', \'theme\' ),
        \'items_list\'                 => __( \'Items list\', \'theme\' ),
        \'items_list_navigation\'      => __( \'Items list navigation\', \'theme\' ),
    );
    $capabilities = array(
        \'edit_post\'          => \'edit_pages\',
        \'read_post\'          => \'edit_pages\',
        \'delete_post\'        => \'edit_pages\',
        \'edit_posts\'         => \'edit_pages\',
        \'edit_others_posts\'  => \'edit_pages\',
        \'delete_posts\'       => \'edit_pages\',
        \'publish_posts\'      => \'edit_pages\',
        \'read_private_posts\' => \'edit_pages\'
    );
    $args = array(
        \'labels\'                     => $labels,
        \'hierarchical\'               => true,
        \'public\'                     => true,
        \'show_ui\'                    => true,
        \'show_admin_column\'          => true,
        \'show_in_nav_menus\'          => true,
        \'show_tagcloud\'              => true,
        \'capabilities\'               => $capabilities,
    );
    register_taxonomy( \'taxonomy_key\', array( \'post\', \'foo_key\', \'bar_key\' ), $args );
}
add_action( \'init\', \'something_taxonomy\', 0 );
我用来从贡献者中删除元框的内容:

if ( is_admin() ) {
    function remove_metabox_for_non_admin_and_editor() {
        if ( current_user_can( \'contributor\' ) || current_user_can( \'subscriber\' ) ) {
            remove_meta_box( \'tagsdiv-foo_key_tag\', \'foo_key\', \'normal\' );
            remove_meta_box( \'categorydiv\', \'foo_key\', \'normal\' );
            remove_meta_box( \'foo_keyimagediv\', \'foo_key\', \'normal\' );
            remove_meta_box( \'authordiv\', \'foo_key\', \'normal\' );
            remove_meta_box( \'trackbacksdiv\', \'foo_key\', \'normal\' );
            remove_meta_box( \'commentstatusdiv\', \'foo_key\', \'normal\' );
            remove_meta_box( \'foo_keycustom\', \'foo_key\', \'normal\' );
            remove_meta_box( \'commentstatusdiv\', \'foo_key\', \'normal\' );
            remove_meta_box( \'commentsdiv\', \'foo_key\', \'normal\' );
            remove_meta_box( \'revisionsdiv\', \'foo_key\', \'normal\' );
            remove_meta_box( \'authordiv\', \'foo_key\', \'normal\' );
            remove_meta_box( \'slugdiv\', \'foo_key\', \'normal\' );
            remove_meta_box( \'taxonomy_keydiv\', \'foo_key\', \'normal\' );
            // remove pages
            remove_menu_page( \'edit-comments.php\' );
            remove_menu_page( \'edit.php\' );
            remove_menu_page( \'tools.php\' );
        }
    }
    add_action( \'admin_menu\', \'remove_metabox_for_non_admin_and_editor\' );
}
找到$id 是我使用Chrome的inspect元素来查找它创建的id的自定义元框。我选择了admin_menu 而不是admin_init 阅读后“What Wordpress hook fires first admin_init or admin_menu“”

SO网友:Tom J Nowell

您想使用admin_init 挂钩:

add_action( \'admin_init\' , \'taxonomy_for_admin_and_editor_only\' );