WordPress:自定义用户角色无法访问自定义 Post 类型 | "Sorry, you are not allowed to access this page"

WordPress: Custom User Role cannot access Custom Post Type | "Sorry, you are not allowed to access this page"

Objective: 创建自定义 post 类型并仅授予管理员和自定义角色查看/控制它的权限。

问题: 对于管理员来说,它工作得很好,但对于我得到的自定义角色: 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
);

自定义角色可以看到 link 到自定义 post 类型,但无法访问它。此外,运行 print_r($wp_roles->get_role( 'supervisor' )->capabilities); 确实表明该角色正确拥有必要的能力。关于如何解决这个问题,我有一些理论,但到目前为止 none 已经成功了。

我的代码如下:

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

收盘中: 我可能只是一个大白痴,忽略了一些明显的东西,但无论如何,我们非常感谢任何和所有的帮助/建议/建议!如果我自己得到答案,我会把它添加到这个讨论中,以帮助其他面临类似问题的人 and/or 我未来的自己 lol

您可以使用 PublishPress Capabilities 插件将多个功能分配给不同的角色。你可以从这里下载这个插件 https://wordpress.org/plugins/capability-manager-enhanced/

解决方案: 玩了一会儿,我意识到我绝对是个白痴,而且想得太多了。虽然我之前阅读并尝试过 this similar post 中的一些内容,但我最终用他们的代码替换了我的代码,并发现它确实适用于我的用例。为了理解为什么会这样,我开始尝试将其转换为我的,并很快找到了问题的根源:

/* 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,
            ));

为了拥有一个干净的自定义菜单,我将 show_in_menu 设置为 false,这给我带来了问题。当我将其更改为 'show_in_menu' => true 时,我的问题就解决了。在解决这个问题时,我很想尝试 remove_menu_page(); 或者考虑更优雅的东西。

无论如何,今天的教训是不要过分关注某一方面。希望这对其他人有帮助并祝您编码愉快!