Wednesday, May 18th, 2022

How to Change Role Name in WordPress – Deluxe Blog Tips

In a recent project, I need to set up a user role. It has the same privileges as the author’s role. So I just use it. But the name “author” is not client friendly.

Writer, contributor and editor are the terms of a publishing website, such as a blog or magazine – for which WordPress is natively built. But WordPress is not the same as it was 5 years ago. WordPress is no more than just a blog or magazine. It can be used for many types of websites.

In my case I use it as foundation for CRM application. Therefore, the words author, contributor, editor are not appropriate. I want to change the name of the author role to “staff”. And here’s 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 is not a good practice to modify role data directly in the same way. It’s also weird to set the name twice. But that’s the only way I found.

i was hoping i could rename the role via 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 does not synchronize the role object with the global data $wp_roles variable. So the name changed in the object does not affect the global data.

Source link