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 Answer
Reset to default 0Apart 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
版权声明:本文标题:Added new role with custom capability. But cannot access the page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741325014a2372423.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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:42my-menu
)? Did you useadd_menu_page()
oradd_submenu_page()
, and can you show your code? – Sally CJ Commented Sep 11, 2021 at 5:47Sorry, you are not authorized to access this page
. I've added more info in the question. I don't think thatadd_menu_page
syntax is correct. – Akshay K Nair Commented Sep 12, 2021 at 7:17