3

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.

  • What are you trying to accomplish with this? Does each role have different capabilities? – Welcher Apr 25 '17 at 2:58
  • I will combine a woocommerce plugin to control visibility product by user role, every each user/role will see a different product. – kyrie Apr 25 '17 at 3:08
  • Every user will have a custom role and only be able to see a single product? – Welcher Apr 25 '17 at 3:10
  • Every user will have a custom role, and I will setting every each product visibility by user role. like this in product setting page: imgur.com/a/du2AI – kyrie Apr 25 '17 at 3:20
  • I've edited my answer to show an example of both creating the new role and assigning the role to the user. As long as you can get the user ID and instantiate a user object to retrieve the login, it should be simple to create and assign a role of the same name. – iyrin Apr 25 '17 at 8:07
1

The user_register hook passes the parameter $user_id to your function. Get the login name after instantiating a user object with get_userdata( $user_id ).

Update the assigned user role with wp_update_user. Alternatively, you can use add_role to assign the new role in addition to the currently assigned role instead of removing the currently defined role with wp_update_user.

add_action( 'user_register', 'abc_assign_role' );

function abc_assign_role( $user_id ) {
    // Instantiate user object.
    $userobj = get_userdata( $user_id );
    // Define new role using the login name of this user object.
    $new_role = $userobj->user_login;

    // Create the role.
    add_role(
        $new_role,
            __( $new_role ),
            array(
            'read'         => true,
            'edit_posts'   => true,
            'delete_posts' => false,
            )
    );

    // Assign a new role to this user, replacing the currently assigned role.
    wp_update_user( array( 'ID' => $user_id, 'role' => $new_role ) );
    // Assign additional role to the user, leaving the default role that is currently assigned to the user.
    // $userobj->add_role( $new_role );
}
| improve this answer | |
0

The function get_userdata() will return a WP_User object so you can access the user_login (aka username). Also, once you have that WP_User object in a variable, you can just do

$variable->add_role()

In full, something like this:

add_action('user_register', 'add_role_to_new_user', 10, 1);
function add_role_to_new_user( $user_id ) {

    $newUser = get_userdata( $user_id ); //WP_User object so we can get the login name

    $newRoleName = $newUser->user_login; // login name in variable

    $newUser->add_role(
        $newRoleName,
        __( $newRoleName ),
        array(
            'read'         => true,  // true allows this capability
            'edit_posts'   => true,
            'delete_posts' => false, // Use false to explicitly deny
            ); //
        }

}

Just to clarify some usage as your needs/intent may dictate approach:

For @iyrin's point of wp_get_current_user, that is returning the id of the currently logged in user. If something alters the registration process (requires double-opt-in, etc.), then does wp_get_current_user have a value? No idea.

get_userdata here is taking the id from the newly registered user, regardless of if they are logged in.

Secondly, using wp_update_user will replace the value of role. add_role will only add the role, not remove any previous role.

So if your sign up gives a role of subscriber, add_role would add, in addition to that, a role with the user's login name. wp_update_user will replace the subscriber role with the user's login name role.

I hope that helps.

| improve this answer | |
  • Hi, is it line15: ) line16: ); ? – kyrie Apr 25 '17 at 3:40
  • Is what line 15 or 16? – hwl Apr 25 '17 at 3:48
  • 1
    Also to add to @iyrin mentioned, you could use wp_get_current_user instead of get_userdata. However, wp_update_user (I believe) will replace the user's role with the one passed to it. add_role does not remove a previously assigned role. – hwl Apr 25 '17 at 3:52
  • I think that is correct. Also note that there are two functions named add_role. One is a function of the WP_User class which allows you to assign the role to the user. The other is a function of the capabilities API and creates the actual role. codex.wordpress.org/Class_Reference/… codex.wordpress.org/Function_Reference/add_role – iyrin Apr 25 '17 at 4:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.