1

目标: 创建自定义帖子类型,仅授予管理员和自定义角色查看/控制它的权限。

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

最后: 我可能只是一个大白痴,忽略了一些明显的事情,但无论如何,任何和所有帮助/建议/建议都非常感谢!如果我自己得到答案,我会将其添加到此讨论中,以帮助其他人解决类似问题和/或我未来的自我哈哈

4

2 回答 2

0

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

于 2021-07-22T04:52:47.760 回答
0

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

/* 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();或者考虑一些更优雅的东西。

无论如何,今天的教训是不要过度关注某一方面。希望这可以帮助其他人并快乐编码!

于 2021-07-22T19:16:05.737 回答