0

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.) enter image description here

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()? 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. 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

1 Answer 1

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');
1
  • 1
    Wow! That worked. I thought only register_post_type managed the capabilities part. Spent hours trying to make it work. Thanks man! Commented Sep 13, 2021 at 5:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.