admin管理员组

文章数量:1287949

Created a new role (price_admin) with the following code and added capabilities. The menu item appeared, but clicking it shows "you are not authorized to access this page".

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');

My register_post_type code is:

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

Edit: I use add_menu_page;

add_menu_page('My Page Title', 'Price Calculator', 'manage_options', 'my-menu', 'show_price', 'dashicons-calculator', '13');

This is how it looks like for my new user; (Cannot access both subpages.)

Created a new role (price_admin) with the following code and added capabilities. The menu item appeared, but clicking it shows "you are not authorized to access this page".

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');

My register_post_type code is:

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

Edit: I use add_menu_page;

add_menu_page('My Page Title', 'Price Calculator', 'manage_options', 'my-menu', 'show_price', 'dashicons-calculator', '13');

This is how it looks like for my new user; (Cannot access both subpages.)

Share Improve this question edited Sep 12, 2021 at 7:15 Akshay K Nair asked Sep 10, 2021 at 6:42 Akshay K NairAkshay K Nair 1911 silver badge11 bronze badges 5
  • 1 Are you sure the user in question have the price_admin role or the capabilities required to manage the posts in your CPT? And just a note that adding custom roles should not be done on every (admin) init, and instead, it should be done upon plugin activation or when switching to another theme. – Sally CJ Commented Sep 10, 2021 at 14:42
  • @SallyCJ I created my new user with this custom role (I only have one custom role, so I'm sure.). I did see something weird, in the user's page in the dashboard, at the bottom there's a section called "Additional Capabilities" and it has "price_admin" written next to Capabilities. Should this show all the capabilities added with add_cap()? – Akshay K Nair Commented Sep 10, 2021 at 15:44
  • 1 Does the error message actually use "authorized" and not "allowed" as in "you are not allowed to"? Can you show how you're adding the admin menu page (my-menu)? Did you use add_menu_page() or add_submenu_page(), and can you show your code? – Sally CJ Commented Sep 11, 2021 at 5:47
  • @SallyCJ , The error is Sorry, you are not authorized to access this page. I've added more info in the question. I don't think that add_menu_page syntax is correct. – Akshay K Nair Commented Sep 12, 2021 at 7:17
  • 1 If the site language was English, then that "authorized" would have been "allowed". But anyway, I hope my answer helps? – Sally CJ Commented Sep 12, 2021 at 13:00
Add a comment  | 

1 Answer 1

Reset to default 0

Apart from what I said in the comments — adding new roles should not be done on every (admin) init (here's why), your code is actually good, but your post type is set to show under the custom admin menu named "Price Calculator" (see the 'show_in_menu' => 'my-menu' in your post type arguments), so you'd want to either add the manage_options capability to specific users like the one in question, or lower the capability required to access that menu, e.g.

// 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');

本文标签: Added new role with custom capability But cannot access the page