我以前从未扮演过角色,所以这是一次有趣的学习经历。我离开这里是因为我花了很多时间研究这个领域,这可能会帮助下一个人。我知道我的问题是关于分类法的,但我对这个问题感到困惑,所以我想从CPT级别进行测试。正确的挂钩是admin_init
然而,它仍在从administrator
和editor
我不想那样。
在我遇到答案之前”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.php
或
edit-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
.
您可以参考法典Administrator 和Editor 在阵列中使用哪些角色。
最终分类法:
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“”