目标: 创建自定义帖子类型,仅授予管理员和自定义角色查看/控制它的权限。
问题:
对于管理员来说,它工作得很好,但对于我得到的自定义角色:
Sorry, you are not allowed to access this page.
起初,我认为这可能只是访问它的能力问题,但这段代码却有所不同:
add_submenu_page( /* STAFF PAGES */
'redacted', //Parent Menu Slug
'Staff Pages', //Page Title text
'Staff Pages', //Menu Title text
'edit_staff', //Capability required for this menu to be displayed by user
'edit.php?post_type=staff' //Link to page
);
自定义角色可以看到自定义帖子类型的链接,但无法访问它。此外,运行print_r($wp_roles->get_role( 'supervisor' )->capabilities);
确实表明该角色正确地拥有必要的能力。我有一些关于如何解决这个问题的理论,但到目前为止还没有一个成功。
我的代码如下:
function initialize_plugin(){
//Non-relevant code redacted
add_action( 'admin_init', array($this, 'admin_init') );
}
function activate(){
$this->custom_post_types();
$this->adjust_user_roles();
//Non-relevant code redacted
}
/* My Custom Post Type */
function custom_post_types(){
register_post_type( 'staff', array(
'labels' => array(
//labels redacted
),
'has_archive' => false,
'hierarchical' => true,
'menu_icon' => 'dashicons-groups',
'capability_type' => array('staff', 'staffs'),
'map_meta_cap' => true,
'public' => true,
'show_in_menu' => false,
'rewrite' => array( 'slug' => 'staff', 'with_front' => false ),
'supports' => array( 'title', 'thumbnail', 'custom-fields', 'revisions'),
'show_in_rest' => true,
'taxonomies' => array( 'member-type' ),
'menu_position' => 2,
));
/* My Custom Role */
function adjust_user_roles(){
$wp_roles = new WP_Roles();
$wp_roles->add_role(
'supervisor', __( 'Supervisor' ),
array(
//General
'moderate_comments' => true,
'upload_files' => true,
//Blog Posts
'read' => true,
'read_post' => true,
'edit_post' => true,
'edit_posts' => true,
'edit_others_posts' => true,
'delete_posts' => false, //Can't delete posts
//Staff (Custom Post Type)
'create_staffs' => true,
'read_staff' => true,
'edit_staff' => true,
'edit_staffs' => true,
'edit_others_staffs' => true,
'edit_published_staffs' => true,
'edit_private_staffs' => true,
'delete_staff' => true,
'delete_others_staffs' => true,
'delete_published_staffs' => true,
'delete_private_staffs' => true,
'publish_staffs' => true,
'read_private_staffs' => true,
)
);
/* Adding to administrator */
function admin_init(){
//Non-relevant code redacted
$this->adjust_user_capabilities("add");
}
function adjust_user_capabilities($action, $roles=array('administrator','editor', 'supervisor')){
$staffCaps = array(
'create_staff',
'read_staff',
'edit_staff',
'edit_staffs',
'edit_others_staffs',
'edit_published_staffs',
'edit_private_staffs',
'delete_staff',
'delete_others_staffs',
'delete_published_staffs',
'delete_private_staffs',
'publish_staffs',
'read_private_staffs',
);
//Cycle through each role
foreach($roles as $roleType) :
$role = get_role( $roleType );
//Add each capability
if($action == "add"){
foreach($staffCaps as $staffCap){
$role->add_cap( $staffCap );
}
}
//Remove each capability
elseif($action == "remove"){
foreach($staffCaps as $staffCap){
$role->remove_cap( $staffCap );
}
}
endforeach;
}
注意:
此代码出现在wp-content/plugins/myplugin/myplugin.php
. 此外,为了清楚起见,我已经编辑了一些不相关的代码部分,例如添加或删除子菜单,并试图解释更多的结构。如果有任何我遗漏的内容或任何人有疑问,请随时告诉我。:-D
最后: 我可能只是一个大白痴,忽略了一些明显的事情,但无论如何,任何和所有帮助/建议/建议都非常感谢!如果我自己得到答案,我会将其添加到此讨论中,以帮助其他人解决类似问题和/或我未来的自我哈哈