我无法获取它,以便我的供应商用户角色可以读取发货过帐类型。它会显示在他们的菜单中,但当你点击它时,你会得到一条错误消息,你不能查看这个页面。
如果我add_cap(\'read_posts\')
但我不想让他们查看常规邮件,只查看发货邮件类型。
我有一个具有以下功能的供应商用户角色。
WP_Role Object
(
[name] => supplier
[capabilities] => Array
(
[read] => 1
[edit_shipment] => 1
[read_shipment] => 1
[edit_others_shipments] => 1
[publish_shipments] => 1
[read_private_shipments] => 1
[edit_shipments] => 1
[create_shipment] => 1
[read_shipments] => 1
)
)
我已经用以下内容设置了装运的邮件类型
function shipment_post_type() {
$labels = array(
\'name\' => _x( \'Shipments\', \'Post Type General Name\', \'sage\' ),
\'singular_name\' => _x( \'Shipment\', \'Post Type Singular Name\', \'sage\' ),
\'menu_name\' => __( \'Shipments\', \'sage\' ),
\'name_admin_bar\' => __( \'Shipments\', \'sage\' ),
\'archives\' => __( \'Shipment Archives\', \'sage\' ),
\'parent_item_colon\' => __( \'Parent shipment:\', \'sage\' ),
\'all_items\' => __( \'All shipments\', \'sage\' ),
\'add_new_item\' => __( \'Add New shipment\', \'sage\' ),
\'new_item\' => __( \'New shipment\', \'sage\' ),
\'edit_item\' => __( \'Edit shipment\', \'sage\' ),
\'update_item\' => __( \'Update shipment\', \'sage\' ),
\'view_item\' => __( \'View shipment\', \'sage\' ),
\'search_items\' => __( \'Search shipments\', \'sage\' ),
\'not_found\' => __( \'Not found\', \'sage\' ),
\'not_found_in_trash\' => __( \'Not found in Trash\', \'sage\' ),
\'featured_image\' => __( \'Featured Image\', \'sage\' ),
\'set_featured_image\' => __( \'Set shipment image\', \'sage\' ),
\'remove_featured_image\' => __( \'Remove shipment image\', \'sage\' ),
\'use_featured_image\' => __( \'Use as shipment image\', \'sage\' ),
\'insert_into_item\' => __( \'Insert into shipment\', \'sage\' ),
\'uploaded_to_this_item\' => __( \'Uploaded to this shipment\', \'sage\' ),
\'items_list\' => __( \'shipments list\', \'sage\' ),
\'items_list_navigation\' => __( \'Constests list navigation\', \'sage\' ),
\'filter_items_list\' => __( \'Filter shipments list\', \'sage\' ),
);
$args = array(
\'label\' => __( \'shipments\', \'sage\' ),
\'description\' => __( \'Manage all shipments, sweepstakes and giveaways.\', \'sage\' ),
\'labels\' => $labels,
\'supports\' => array( \'revisions\' ),
\'taxonomies\' => array( \'\' ),
\'hierarchical\' => false,
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'menu_position\' => 5,
\'menu_icon\' => \'dashicons-archive\',
\'show_in_admin_bar\' => true,
\'show_in_nav_menus\' => false,
\'can_export\' => true,
\'has_archive\' => false,
\'exclude_from_search\' => true,
\'publicly_queryable\' => true,
\'map_meta_cap\' => true,
\'capabilities\' => array(
\'edit_post\' => \'edit_shipment\',
\'read_post\' => \'read_shipment\',
\'read_posts\' => \'read_shipments\',
\'delete_post\' => \'delete_shipment\',
\'delete_posts\' => \'delete_shipments\',
\'edit_posts\' => \'edit_shipments\',
\'edit_others_posts\' => \'edit_others_shipments\',
\'publish_posts\' => \'publish_shipments\',
\'read_private_posts\' => \'read_private_shipments\',
\'create_posts\' => \'create_shipments\',
),
);
register_post_type( \'shipment\', $args );
}
add_action( \'init\', \'shipment_post_type\', 0 );
SO网友:Myles
您的自定义帖子类型看起来设置正确。它在我的测试安装中起作用。请尝试此操作,而不是您当前使用的任何add\\u角色和add\\u cap代码。(仅用于测试目的。请勿在生产代码中使用它,原因如下。)这对我有用:
function prefix_set_up_supplier_role(){
remove_role( \'supplier\' );
add_role( \'supplier\', \'Supplier\', array(
\'read\' => true,
\'edit_shipment\' => true,
\'read_shipment\' => true,
\'read_shipments\' => true,
\'delete_shipment\' => true,
\'delete_shipments\' => true,
\'edit_shipments\' => true,
\'edit_others_shipments\' => true,
\'publish_shipments\' => true,
\'read_private_shipments\' => true,
\'create_shipments\' => true,
)
);
}
add_action( \'init\', \'prefix_set_up_supplier_role\' );
记住,添加用户角色和功能实际上会将数据保存到数据库中,这一点非常重要。因此,如果之前有一个版本的代码不能正常工作,然后添加了一些可以使其正常工作的内容,那么如果数据库中仍然有旧数据,则可能不会生效。如果数据库中已存在该角色,add\\u role()将返回null。对于生产代码,实际上应该为这些东西使用插件激活和停用挂钩,而不是每次都运行它,如下所示:
register_activation_hook( __FILE__, \'prefix_activate\' );
function prefix_activate(){
add_role( \'supplier\', \'Supplier\', array(
\'read\' => true,
\'edit_shipment\' => true,
\'read_shipment\' => true,
\'read_shipments\' => true,
\'delete_shipment\' => true,
\'delete_shipments\' => true,
\'edit_shipments\' => true,
\'edit_others_shipments\' => true,
\'publish_shipments\' => true,
\'read_private_shipments\' => true,
\'create_shipments\' => true,
)
);
}
register_deactivation_hook( __FILE__, \'prefix_deactivate\' );
function prefix_deactivate(){
remove_role( \'supplier\' );
}