Skip to:
Content
Pages
Categories
Search
Top
Bottom

Setting User Role During Custom Registration

Viewing 9 replies - 1 through 9 (of 9 total)
  • @henrywright

    Moderator

    Hi @ch1n3s3b0y

    Are you sure $POST_['user_type'] is set?

    switch( $POST_['user_type'] ) {
        case 'customer':
            $new_role = 'customer';
            break;
        case 'guru':
            $new_role = 'expert';
            break;
        default:
            $new_role = 'user_role_is_neither_customer_or_expert';
    }

    Try adding a default case to your switch statement like I’ve shown above. Then check in the wp_users table in your database to see what role is assigned.

    @ch1n3s3b0y

    Participant

    Hi @henrywright – I set it up as you said above and also set a default via user profile editor as ‘contributor’. The user was registered as a contributor even though it should have been an expert.

    Do you think the POST data isn’t getting to this stage? I don’t know how to test it since I can’t echo what’s happening at this stage.

    @ch1n3s3b0y

    Participant

    @henrywright – I wrote

    $POST_['user_type']

    instead of

    $_POST['user_type']

    D’oh! It still isn’t working though, kind of. The user is actually having the role set during the registration and is showing up in the admin section in Users as an expert, as well as pending. When I activate the account from admin they then get set as a subscriber (the default set by user roles editor). So it appears there’s another step happening after the activation of the account.

    Any ideas what I need to look at?

    @ch1n3s3b0y

    Participant

    Ok, I have this working! For anyone else struggling with this, these are the steps I took:

    1. I created a custom registration form ( I needed more than one form for my setup) using the tutorial from Brajesh Singh http://buddydev.com/buddypress/show-buddypress-user-registration-form-everywhere-buddypress-site/

    2. I added a hidden input field to my form:

    <input type="hidden" name="user_type" id="user_type" value="your_role" />

    3. I added a user_meta during the registration to store the role for later use during the activation process:

    function store_user_role($user_id) {
        $user_type = $_POST['user_type'];
        $user_role = $user_type;
        switch($user_role) {
            case "your_role":
                add_user_meta( $user_id, '_member_role', 'yourrole');
                break;
            case "my_role":
                add_user_meta( $user_id, '_member_role', 'myrole');
                break;
    	default:
                add_user_meta( $user_id, '_member_role', 'defaultrole');
        }
    }
    add_action( 'bp_core_signup_user', 'store_user_role', 20, 1);

    4. Then during the activation process you check to see which user role is stored in the user_meta, and assign that as the actual WP user role:

    add_action('bp_core_activated_user', 'bp_custom_registration_role',10 , 3);
    function bp_custom_registration_role($user_id, $key, $user) {
       $userdata = array();
       $userdata['ID'] = $user_id;
       $key = '_member_role';
       $single = 'true';
       $userRole = get_user_meta( $user_id, $key, $single );
       
       if ($userRole == 'yourrole') 
          $userdata['role'] = 'yourrole';
       
       if ($userRole == 'myrole') 
          $userdata['role'] = 'myrole';
    
       if (($userdata['role'] == "yourrole") or ($userdata['role'] == "myrole"))
          wp_update_user($userdata);
       
      }

    That’s it. I used a few different examples from these support forums so credit goes out to those who got this stuff working in the first place. Hope this helps someone else.

    @henrywright

    Moderator

    The user was registered as a contributor even though it should have been an expert.

    This shows that $_POST['user_type'] is either a) not set or b) set to something that isn’t ‘expert’. This shows, your problem isn’t with your switch statement, or your wp_update_user() function; instead, the problem is with your $_POST data. You’ll need to investigate how you’re capturing data from your form.

    @ch1n3s3b0y

    Participant

    @henrywright – You didn’t see my post afterwards. I managed to get it all working 🙂

    @henrywright

    Moderator

    Yes, sorry I didn’t see your latest post.

    Great to see you got it working! 😀

    @klame

    Participant

    Seems great 🙂
    But I’m really confused how it’s working..
    I don’t understand all that myrole, yourrole and _member_role.

    The script are supposed to go to bp_custom.php, right?

    @ch1n3s3b0y

    Participant

    @klame – Hey. Yep, it goes in bp_custom.php, though I think you could also put it into your functions.php if you wanted (depends if you want the same functionality if you change theme).

    Keep _member_role as it is. The ‘yourrole’ and ‘myrole’ are the names of your user roles that you created, e.g. ‘subscriber’, ‘administrator’ etc.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Setting User Role During Custom Registration’ is closed to new replies.
Skip to toolbar