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..
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
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
Method 3
suppose you want to clone the editor.
$edr = $wp_roles->get_role('Editor');
add_role('clonerole', 'clone roles', $edr->capabilities);
Method 4
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
}
}
?>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0