• Resolved thrasherstudios77

    (@thrasherstudios77)


    Hi,

    I need to programmatically add an existing role to a member. I see in the backend / admin Users->Profile page under the ‘User Roles’ area there is a way to check which roles a user/memeber can have, if you are the administrator. However, we don’t want to have to manually add an additional role each time there is a new registration.

    Currently when a new user signs up they have a Vendor role but we also want them to have the Instructor role at the same time.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Justin Tadlock

    (@greenshady)

    So, you want to be able to set the default role to multiple roles upon user registration?

    That’s actually a really cool idea. I’m surprised I haven’t thought of it before. I might just roll that up into an add-on plugin or as a part of Members. It’s definitely going on my list either way.

    I’m not entirely sure how to do it programmatically just yet though.

    Thread Starter thrasherstudios77

    (@thrasherstudios77)

    Thanks Justin

    Could you explain a little bit how the ‘role_update()’ function works.

    If possible I would like to add to the ‘user_register’ hook to add to the current users role array something like this (This doesn’t work for me though).
    $user = wp_get_current_user();
    $user_roles = (array) $user->roles;
    $user->roles[] = ‘instructor’;

    Justin Tadlock

    (@greenshady)

    You’d want to use the $user->add_role() method of WP_User. Just a quick solution off the top of my head would be something like:

    
    add_action( 'user_register', function( $user_id ) {
    
    	$user = new WP_User( $user_id );
    
    	$user->add_role( 'example' );
    } );
    
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Add existing role dynamically’ is closed to new replies.