admin管理员组文章数量:1387460
I am writing a plugin that offers an admin menu item which only a specific, custom user role ("customrole" in the example) can access. I have implemented this as follows, and it works:
function add_admin_menu() {
add_menu_page(
'Custom-Plugin',
'Custom-Plugin',
'customrole',
'custom-plugin',
'init_custom_menu_page'
);
}
The problem is that the administrator does not have the rights to access this menu item anymore; I would like administrators to still be able to access it, however (and the "customrole"-users). How can I achieve this?
I am using the "Members" plugin to create custom user roles if that makes a difference.
Thanks in advance!
I am writing a plugin that offers an admin menu item which only a specific, custom user role ("customrole" in the example) can access. I have implemented this as follows, and it works:
function add_admin_menu() {
add_menu_page(
'Custom-Plugin',
'Custom-Plugin',
'customrole',
'custom-plugin',
'init_custom_menu_page'
);
}
The problem is that the administrator does not have the rights to access this menu item anymore; I would like administrators to still be able to access it, however (and the "customrole"-users). How can I achieve this?
I am using the "Members" plugin to create custom user roles if that makes a difference.
Thanks in advance!
Share Improve this question asked Apr 11, 2020 at 13:25 YatoYato 175 bronze badges1 Answer
Reset to default 0You shouldn't use Roles to handle permissions for your menu page. Instead, use capabilities, and then assign those capabilities to whichever roles should have access.
For example, instead of creating a customrole
role, use a custom capability like manage_custom_plugin
:
add_menu_page(
'Custom-Plugin',
'Custom-Plugin',
'manage_custom_plugin',
'custom-plugin',
'init_custom_menu_page'
);
Now, using the Members plugin you can enter manage_custom_plugin
into the "Custom Capability" box, and add it to whichever roles you need.
In code you would grant this capability to roles using WP_Roles::add_cap()
, like so:
$roles = wp_roles();
$roles->add_cap( 'administrator', 'manage_custom_plugin' );
$roles->add_cap( 'editor', 'manage_custom_plugin' );
Just be aware that these functions write to the database, so should only be run once, on plugin or theme activation.
本文标签: grant multiple roles access to specific admin menu item
版权声明:本文标题:grant multiple roles access to specific admin menu item 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744575491a2613598.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论