添加了具有自定义功能的新角色。但无法访问该页面

时间:2021-09-10 作者:Akshay K Nair

创建了新角色(price_admin) 使用以下代码和添加的功能。菜单项出现了,但单击它会显示;您无权访问此页面;。

function ui_new_role() {  
    add_role(\'price_admin\',
            \'Price Calculator Admin\',
            array(
                \'read\' => true,
                \'edit_posts\' => true,
                \'delete_posts\' => true,
                \'publish_posts\' => true,
                \'upload_files\' => true,
            )
        );
        $roles = array(\'price_admin\',\'editor\',\'administrator\');
        foreach($roles as $the_role) { 
             $role = get_role($the_role);
                 $role->add_cap( \'read\' );
                 $role->add_cap( \'read_price\');
                 $role->add_cap( \'edit_price\' );
                 $role->add_cap( \'edit_prices\' );
                 $role->add_cap( \'edit_others_prices\' );
                 $role->add_cap( \'edit_published_prices\' );
                 $role->add_cap( \'publish_prices\' );
        }
}
add_action(\'admin_init\', \'ui_new_role\');
我的register_post_type 代码为:

             register_post_type(
                    \'price\',
                    array(
                        \'labels\' => array(\'name\' => \'Treatment Questions\'),
                        \'capability_type\'     => array(\'price\',\'prices\'),
                        \'map_meta_cap\'        => true,
                        \'public\' => true,
                        \'show_ui\' => true,
                        \'show_in_menu\' => \'my-menu\',
                        \'menu_position\' => 15,
                        \'supports\' => array(\'title\'),
                        \'taxonomies\' => array(\'location\'),
                        \'has_archive\' => true,
                    )
                );
编辑:我使用add_menu_page;

add_menu_page(\'My Page Title\', \'Price Calculator\', \'manage_options\', \'my-menu\', \'show_price\', \'dashicons-calculator\', \'13\');
这就是我的新用户的样子;(无法访问两个子页面。)enter image description here

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

除了我在评论中所说的之外,不应该在每个(管理)初始化上添加新角色(here\'s why), 您的代码实际上很好,但您的帖子类型设置为显示在名为“自定义管理”菜单下;价格计算器“;(请参见\'show_in_menu\' => \'my-menu\' ,因此您可以添加manage_options 特定用户的功能,如所讨论的用户,或访问该菜单所需的功能,例如。

// I changed the 3rd parameter from manage_options to edit_prices.
add_menu_page(\'My Page Title\', \'Price Calculator\', \'edit_prices\', \'my-menu\', \'show_price\', \'dashicons-calculator\', \'13\');

相关推荐