avatar Deluxe Blog Tips About Projects

How to change a role name in WordPress

In a recent project, I need to setup a user role. It has the same privileges as the author role. So I just use it. But the name "author" is not friendly to the client.

Authors, contributors and editors are terms of a publishing website, like a blog or a magazine - which WordPress is originally created for. But WordPress is not the same as it was 5 years ago. WordPress is now more than just a blog or a magazine. It can be used for many types of websites.

In my case I use it as a foundation for a CRM application. So, the terms authors, contributors, editors are not suitable. I'd like to change the name of the author role to "Employee". And here is how I do it.

add_action( 'wp_roles_init', function( $wp_roles ) {
    $wp_roles->roles['author']['name'] = 'Employee';
    $wp_roles->role_names['author'] = 'Employee';
} );

It's not a good practice to modify the roles data directly like that. It's also weird to set the name twice. But that's the only way I found.

I expected I can change the role name via the WP_Role object, somehow like this:

$role = get_role( 'author' );

$role->set_name( 'Employee' );
// or
$role->name = 'Employee';

Unfortunately, it doesn't work. While we can run the code without errors, WordPress doesn't synchronize the role object with the global data in $wp_roles variable. So the name changed in the object doesn't affect the global data.

🔥 HOT: Interested in boosting your WordPress SEO? My Slim SEO plugin is a super lighweight and automated plugin that handles most the hard work for you: meta tags, sitemap, redirection, schema & link building.

👉 Have a look and you will love it: wpslimseo.com