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..

up vote 26 down vote accepted

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. – spankmaster79 Jan 21 '14 at 17:21
  • @spankmaster79 Yes please, that will be great! – Rutwick Gangurde Jan 22 '14 at 6:00
  • @RutwickGangurde didn't work any better. Role is still ignored by the plugin – spankmaster79 Jan 24 '14 at 15:42

You could always use the User Role Editor plugin;

  1. Install the plugin
  2. Go to Users > User Role Editor
  3. Click "Add Role" to the right
  4. Choose the role you wish to duplicate from the "Make copy of" dropdown in the dialogue box
  5. 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
  }
}
?>

To Clone Editor Role You can User below Code and Past the Following Coste at function.php

add_action('init', 'CreatecloneRoleEditor');
function CreatecloneRoleEditor()
{
    global $wp_roles;
    if ( ! isset( $wp_roles ) )
        $wp_roles = new WP_Roles();

    $etr = $wp_roles->get_role('Editor');
    $wp_roles->add_role('new_Editor', 'new Editor', $etr->capabilities);
}

FOr more Infomation to Create Clone administration role ,Editor etc you can following this Link http://wpdadd.com/create-new-roles-using-clone-wordpress/

  • Your answer is just a copy of the accepted answer. – RRikesh Sep 1 '17 at 5:46

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.