How to Change Default Role Names in WordPress

Home » Snippets » How to Change Default Role Names in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

When you set up a new WordPress site, there are five default user roles: Administrator, Editor, Author, Contributor, and Subscriber. These roles define what a user can and cannot do on your site. While the default role names are sufficient for most sites, there may be situations where you want to change them to better suit your needs. In this article, we’ll show you how to change the default role names in WordPress, so you can create custom roles that better fit your site’s specific requirements.

					function wpturbo_change_role_names() {
    global $wp_roles;

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

    // Change "Administrator" role name
    $wp_roles->roles['administrator']['name'] = __( 'Manager', 'wpturbo' );

    // Change "Editor" role name
    $wp_roles->roles['editor']['name'] = __( 'Content Manager', 'wpturbo' );

    // Change "Author" role name
    $wp_roles->roles['author']['name'] = __( 'Writer', 'wpturbo' );

    // Change "Contributor" role name
    $wp_roles->roles['contributor']['name'] = __( 'Journalist', 'wpturbo' );

    // Change "Subscriber" role name
    $wp_roles->roles['subscriber']['name'] = __( 'Follower', 'wpturbo' );
}
add_action( 'init', 'wpturbo_change_role_names' );
				

The code snippet above allows you to change the default role names in WordPress for the Administrator, Editor, Author, Contributor, and Subscriber roles. This is especially helpful if you are developing a custom project where you need specific roles or if you want to create custom role names that are more descriptive of their responsibilities.

The code first defines a new function called wpturbo_change_role_names(). This function utilizes the $wp_roles global variable, which contains all of the built-in WordPress role names and capabilities. If $wp_roles isn’t set, then the default WP_Roles() object is created.

To change the name of a role, we access the $wp_roles->roles variable and find the appropriate role by its key. The role key is usually the lowercase version of the role name with spaces replaced by underscores (e.g. ‘administrator’, ‘editor’, ‘author’, etc.).

To change the name of a role, we update the name field of the respective role. For example, to change the name of the ‘Administrator’ role to ‘Manager’, we update the name field like this:

$wp_roles->roles['administrator']['name'] = __( 'Manager', 'wpturbo' );

The __('Manager', 'wpturbo') code indicates that the string ‘Manager’ should be translated and the text domain is ‘wpturbo’. You can change these values to suit your project’s needs.

The same process is repeated for the other default roles. Once all the changes are made, we hook the function to the init action using add_action(). This ensures that the changes take effect when WordPress is initialized.

That’s it! This code snippet is a simple and effective way to change the default role names in WordPress.

Register an account to save your snippets or go Pro to get more features.