How to create new role with same capabilities of existing role. Eg: I would like to create a new role with same capabilities of administrator or editor and so on..
4 Answers
Try this... This should work.
<?php
add_action('init', 'cloneRole');
function cloneRole()
{
global $wp_roles;
if ( ! isset( $wp_roles ) )
$wp_roles = new WP_Roles();
$adm = $wp_roles->get_role('administrator');
//Adding a 'new_role' with all admin caps
$wp_roles->add_role('new_role', 'My Custom Role', $adm->capabilities);
}
?>
Check it.
-
Watchout!! this seems to clone the role before the plugins add roles and capabilities. I'll try admin_init to see if this is better. Jan 21, 2014 at 17:21
-
-
@RutwickGangurde didn't work any better. Role is still ignored by the plugin Jan 24, 2014 at 15:42
You could always use the User Role Editor plugin;
- Install the plugin
- Go to Users > User Role Editor
- Click "Add Role" to the right
- Choose the role you wish to duplicate from the "Make copy of" dropdown in the dialogue box
- Click "Add Role" in the dialogue box
suppose you want to clone the editor.
$edr = $wp_roles->get_role('Editor');
add_role('clonerole', 'clone roles', $edr->capabilities);
the system that worked in my case is this:
<?php
add_action('init', 'cloneRole');
function cloneRole() {
$adm = get_role('administrator');
$adm_cap= array_keys( $adm->capabilities ); //get administator capabilities
add_role('new_role', 'My Custom Role'); //create new role
$new_role = get_role('new_role');
foreach ( $adm_cap as $cap ) {
$new_role->add_cap( $cap ); //clone administrator capabilities to new role
}
}
?>