admin管理员组文章数量:1392076
I've created a new role and I need to give this role the full permissions and capability of an admin less create new admin user and edit plugin/themes. Is possible recall all and remove only the 3 I need, or i have to put 1 by 1 like in the code i posted (edit/delete post)?
$result = add_role( 'Client', 'Client', array(
'read' => true,
'edit_posts' => true,
'delete_posts' => false,
) );
I've created a new role and I need to give this role the full permissions and capability of an admin less create new admin user and edit plugin/themes. Is possible recall all and remove only the 3 I need, or i have to put 1 by 1 like in the code i posted (edit/delete post)?
$result = add_role( 'Client', 'Client', array(
'read' => true,
'edit_posts' => true,
'delete_posts' => false,
) );
Share
Improve this question
edited Feb 3, 2020 at 15:28
DevBeppe
asked Feb 3, 2020 at 15:19
DevBeppeDevBeppe
477 bronze badges
1 Answer
Reset to default 3It sounds like you want to add all the admin capabilities to a role except a few key capabilities that you have in mind. What you could do is get the administrator role and do a key diff on capabilities you do not want. We have to use array_diff_key()
because capabilities are keyed by name:
array( 'edit_plugins' => 1 )
You would only want to do this once though. Either whenever your plugin has been installed or you can do a check if the role exists and return early. Otherwise, whenever the admin role gets new capabilities you may find the new role also gets those same capabilities. An example of that check could look like:
if( role_exists( 'Client' ) ) {
return
}
Below is the function without the above conditional which shows an example of how you could do this:
/**
* Adds are new role based on the administrator role capabilities
*
* @return void
*/
function wpse357776_new_role() {
$admin_role = get_role( 'administrator' );
// Array of capabilities you do not want the new role to have.
$remove_caps = array(
'switch_themes' => 0,
'edit_themes' => 0,
'activate_plugins' => 0,
'edit_plugins' => 0,
'manage_options' => 0,
);
// Run a diff on the admin role capabilities and the removed rules
$my_role_caps = array_diff_key( $admin_role->capabilities, $remove_caps );
// Add the role
$my_role = add_role( 'Client', 'Client', $my_role_caps );
}
add_action( 'after_setup_theme', 'wpse357776_new_role' );
本文标签: pluginsHow create a role with admin capability less 1 or 2
版权声明:本文标题:plugins - How create a role with admin capability less 1 or 2? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744780940a2624708.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论