29

Is there anyway to rename a user role name via hook, instead of using plugin?

Edit

For example, administrator » owner

2
  • Could you elaborate on what you want to achieve?
    – BjornW
    Jul 15, 2011 at 15:50
  • rename user role name, that's it. for example, administrator -> owner
    – Sean Lee
    Jul 15, 2011 at 17:46

6 Answers 6

48
function change_role_name() {
    global $wp_roles;

    if ( ! isset( $wp_roles ) )
        $wp_roles = new WP_Roles();

    //You can list all currently available roles like this...
    //$roles = $wp_roles->get_names();
    //print_r($roles);

    //You can replace "administrator" with any other role "editor", "author", "contributor" or "subscriber"...
    $wp_roles->roles['administrator']['name'] = 'Owner';
    $wp_roles->role_names['administrator'] = 'Owner';           
}
add_action('init', 'change_role_name');

http://www.garyc40.com/2010/04/ultimate-guide-to-roles-and-capabilities/

3
  • I couldn't get this to work. Regardless of what I do, then it's still called 'Subscribers' in the backend creation process. It looks like that $wp_roles now has a [role_objects] part of it's array.
    – Zeth
    Mar 16, 2019 at 16:06
  • This did something very strange. It only affected the first letter ("Odministrator")!
    – Mark
    Jun 4, 2019 at 16:19
  • This solution will not update the db entry. You are updating the name for the current instance, if this action does not fire, it can mess up your logic. There is no method in Wp_Role to save. Best way is to $roles = get_option('user_roles'); $roles['administrator']['name'] = "Owner"; update_option('user_roles', $roles); that snipped will only need to be ran once. Feb 9, 2022 at 3:18
6

If you are using WP version 4.7+ you may accomplish this using the wp_roles_init action like so:

add_action( 'wp_roles_init', static function ( \WP_Roles $roles ) {
    $roles->roles['administrator']['name'] = 'Owner';
    $roles->role_names['administrator'] = 'Owner';
} );
2
  • 1
    This is the best answer
    – OsDev
    Aug 31, 2021 at 19:25
  • 1
    Tip: This code works inside a plugin or mu-plugin. It does not work inside a child-theme, as the child theme is loaded too late for the wp_roles_init hook.
    – Philipp
    Mar 24, 2022 at 16:45
5

Actually, there are many ways to achieve that:

With pure php and mysql you can edit the serialized entry in the db. Indeed, Wordpress stores the serialized array of roles in wp_options table.

So:

  1. Fetch the serialized array:
    SELECT option_value as serialized_string FROM wp_options WHERE option_name = 'wp_user_roles';
  2. Unserialize the string – php: $rolesArray = unserialize($serialized_string)
  3. Change the role name – php: $rolesArray['role_key']['name'] = "New name"
  4. Serialize back the array – php: echo serialize($rolesArray)
  5. Replace the db option_value content with output from the previous point

If you feel confident with Wordpress, you can even use the embedded Wordpress REPL in wp-cli to fetch the stored value with get_option('wp_user_roles') and then use update_option to update it.

And (always) remember to backup the db before options manipulation ;)


Otherwise, if you don’t care about role_key value…

…it’s very easy to achieve that with wp-cli:

  1. duplicate the existing role – $ wp role create new_role 'New Role' --clone=old_role
  2. delete the old one – $ wp role delete old_role
  3. then associate new_role to the user(s).
  4. eventually repeat step 1 and 2 until old_role = new_role
3

A simple solution would be to just add a user role using add_role, that way you can name it whatever you want and add whatever capabilities you want. http://codex.wordpress.org/Function_Reference/add_role

1
  • Tried to avoid this, but I guess it's the cleanest way to do without plugin.
    – Sean Lee
    Jul 15, 2011 at 22:16
1

You can create a custom localization file. Get this file: http://svn.automattic.com/wordpress-i18n/pot/trunk/wordpress.pot and edit using PoEdit tool (for example). In next step save localization file as en_GB.mo (or other) and edit wp-config file:

define ("WPLANG", "en_GB");

1
  • 1
    Already tried this method, but some pull down menus were not translated.
    – Sean Lee
    Jul 15, 2011 at 22:15
0

You can edit it directly in your DB, and it will be edited permanently for your website. Here is where WP keeps user roles

SELECT * from blog_options WHERE option_name = 'blog_user_roles'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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