how to change wordpress role name without plugin

how to change wordpress role name without plugin

There are many roles in wordpress (Administrator, Editor, Author, Contributor, Subscriber)  If you want change default role names as per your project requirement. There are many reasons where we wanted to change role name and capabilities of wordpess users.

how to change wordpress role name

 

how to change wordpress role name without plugin
how to change wordpress role name without plugin

Using simple code you can change worpdress role names. You just need to put following code in your functions.php file. This file you can find in wordpress theme folder.

function wpapi_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', 'wpapi_change_role_name');

Using above you you will be able to change role names. Here using able above code, administrator role name will be replaced with Owner role name.

If you want to add new role to your wordpress website than use following simple code. As per above code, following code also, you need to copy paste in functions.php file.

$result = add_role(
    'basic_contributor',
    __( 'Basic Contributor' ),
    array(
        'read'         => true,  // true allows this capability
        'edit_posts'   => true,
        'delete_posts' => false, // Use false to explicitly deny
    )
);
if ( null !== $result ) {
    echo 'Yay! New role created!';
}
else {
    echo 'Oh... the basic_contributor role already exists.';
}

Using above code you will create the “Basic Contributor” Role in wordpress website. You can use above code in wordpress theme or plugin. There are many wordpress hooks which really helpful for managing wordpress roles and capabilities.

If you have any doubts or question than write comments bellow.

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

Leave a Reply

Your email address will not be published.