Forums

How can I programmatically assign a role to a person? (3 posts)

  1. spstieng
    Member
    Posted 1 month ago #

    This is a 2 part question.

    1. How to I programmatically assigna role to a user?
    2. How can I create a hook that assigns a role to a user on user registration?

    In my user registration, the user must select from a dropdown list, what kind of user he / she is. Then during the processing of the registration, I need to assign the corect role.

  2. spstieng
    Member
    Posted 1 month ago #

    Ok, I found 2 sites which may be to some help.
    This one and this one.

    A snippet from the first link:

    The best way to understand what a hook does is to look at where it occurs in the source code.
    Action hooks look like this: do_action( "hook_name" )
    Filter hooks look like this: apply_filters( "hook_name", "what_to_filter" ).
    Remember, this hook may occur in more than one file. Moreover, the hook's context may change from version to version.

    if ( isset($role) ) {
    	           $user = new WP_User($user_id);
    	           $user->set_role($role);
    	      } elseif ( !$update ) {
    	           $user = new WP_User($user_id);
    	           $user->set_role(get_option('default_role'));
    	      }
    
    	      wp_cache_delete($user_id, 'users');
    	      wp_cache_delete($user_login, 'userlogins');
    
    	      if ( $update )
    	           do_action('profile_update', $user_id, $old_user_data);
    	      else
    	           do_action('user_register', $user_id);
    
    	      return $user_id;
    	 }

    But how exactly can I take advantage of this?
    I'm guessing that in my plugin, I have to make some code that gets selected user role, and assign this to the user. But how to accomplish this, I'm not sure.

    Any suggestions?

  3. scdwb
    Member
    Posted 1 week ago #

    Hey,

    I'm looking for a very similar solution. I'll be using it on separate forms but the same solution should be able to be applied to a single form via a drop down like yours.

    Heres my post:

    http://wordpress.org/support/topic/332741

    I've got in contact with one member who say they can help. Will post any findings. Unless you have solved this? If so please tell us how!

    Thanks :-)

Reply

You must log in to post.

About this Topic

Tags