I’m trying to do custom function when new user register then adds new user role, new user role is the username.

For example, when user register and the username is apple, It will automatically create a role called Apple and assign to Apple user.

add_action( 'user_register', 'add_new_role' );

function add_new_role() {
 $result = add_role(
    __( 'user_name' ),
        'read'         => true,  // true allows this capability
        'edit_posts'   => true,
        'delete_posts' => false, // Use false to explicitly deny


This code can be implemented when a new user is registered and create a role called username, I don’t know how to get the username from the form and I want it automatically assign the new role to the new user.

Thank you for the help.

Read more here: When new user register then add new user role


If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Wordpress related questions and answers: