When new user register then add new user role

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',
    __( 'user_name' ),
    array(
        '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

Leave a Reply

Your email address will not be published. Required fields are marked *