Buddypress – when user activates account,user role changes to default

Iam working with buddypress,
I have a two user roles,
1-student
2-faculty
and i have set default user role as subscriber.
when user registers and activates account by clicking on link sent through mail.User role changes to default(subscriber).

Any idea what is the issue? Below is the code assigning role to user on sign up.

add_action('bp_core_signup_user', 'ad_user_signup_usermeta', 10, 5);
function ad_user_signup_usermeta($user_id, $user_login, $user_password, $user_email, $usermeta) {

        if(isset($_POST['signup_membership']) && !empty($_POST['signup_membership']))
        update_user_meta($user_id, 'membership', $_POST['signup_membership']);

        $userdata = array();
        $userdata['ID'] = $user_id;
        if(!empty($_POST['signup_usertype'])) {
            if($_POST['signup_usertype'] == 'student') {
            $userdata['role'] = 'student';  
            }
            if($_POST['signup_usertype'] == 'instructor') {
            $userdata['role'] = 'instructor';   
            }   
        }

        if ($userdata['role']){
            wp_update_user($userdata);
        }

}

Related posts

Leave a Reply

1 comment

  1. Upon activation, BuddyPress (at least version 2.0.2) updates the user’s role to the default role.

    https://buddypress.trac.wordpress.org/browser/tags/2.0.2/bp-members/bp-members-functions.php#L1560

    You can comment out that line, or write some code to work around it. I’m using “WP Roles At Registration” and ran across the same problem. I ended up adding a filter on bp_core_signup_user to save the original role but you’ll want to add something like this to your ad_user_signup_usermeta:

    update_user_meta($user_id, 'temp_role', $role_name)
    

    then reset it back in a filter for bp_core_activated_user

    public function after_bp_activated_user($user_id, $key, $user) {
        $user = get_userdata($user_id);
        $role = get_user_meta($user_id, 'temp_role');
        if ($role) {
            $user->set_role($role[0]);
        }
    }
    add_filter('bp_core_activated_user', array($this, 'after_bp_activated_user'), 30, 3);