Skip to:
Content
Pages
Categories
Search
Top
Bottom

Setting up activated users with stored metadata

  • @xtremefaith

    Participant

    I have a custom registration form that tweaked the registration.php template to include a couple additional inputs. The form works as expected, being processed through the buddypress channels

    bp_core_screen_signup()

    Then I hook into the bp_core_signup_user like so:

    # Capture registration form's metadata fields
    add_action('bp_core_signup_user', array( $this, 'add_registration_metadata' ),10 ,1);

    Through my custom add_registration_metadata() method I store the $_POST meta to the user_id like so:

    public function add_registration_metadata($user_id){
    	$type = isset($_POST['team_type']) ? $_POST['team_type'] : FALSE;
    	if ($type){
    		# Make sure that the post value matches the available options
    		if (($type == "type1") || ($type == "type2"))
    			# Save the value as user meta
    			add_user_meta( $user_id, 'team_type', $type);
    	}
    	/* Save username to metadata */
    	$username = isset($_POST['signup_username']) ? $_POST['signup_username'] : FALSE;
    	/* Save first name to metadata */
    	$fname = isset($_POST['signup_fname']) ? $_POST['signup_fname'] : FALSE;
    	/* Save last name to metadata */
    	$lname = isset($_POST['signup_lname']) ? $_POST['signup_lname'] : FALSE;
    
    	/* Update WordPress Details */
    	$userdata = array (
    		'ID' => $user_id,
    		'first_name' => $fname,
    		'last_name' => $lname,
    		'nickname' => $username,
    		'user_nicename' => DIV_Helper::slugify($username)
    	);
    	wp_update_user( $userdata );
    }

    As expected all the values are set in the usermeta table. I would think this is all that is left to do but its not and there is some confusion as to what is happening next.

    So now the user attempts to activate their account through their email obviously passing the activation key. This works! BUT my metadata values are gone. For some reason BuddyPress wipes the values stored in the usermeta table. So then I try to hook into the bp_core_activate_signup like so:

    add_action('bp_core_activated_user', array( $this, 'setup_registration_metadata') );

    This custom method attempts to re-update the user information but for some reason no combination seems to work, here’s what I have at the moment:

    public function setup_registration_metadata($user){
    	if (! empty( $user ) ) {
    		if ( is_array( $user ) ) {
    			$user_id = $user['user_id'];
    		} else {
    			$user_id = $user;
    		}
    	
    		if ( ! empty( $user_id ) ) {
    			$usermeta = get_user_meta($user_id);	
                                
                                // This doesn't seem to work as expected	
    			$type = $usermeta['team_type'];
                                // But this does...
                                $type = get_user_meta($user_id, 'team_type' TRUE);
    
    			# Make sure the value matches the available options
    			if (($type == "type1") || ($type == "type2"))
    				# Set the member type
    				bp_set_member_type($user_id, $type);
    
    			# Setup username
    			$username = $usermeta['username'][0];
    			# Setup first name
    			$fname = $usermeta['first_name'][0];
    			# Setup last name
    			$lname = $usermeta['last_name'][0];
    			
    			/* Update WordPress Details */
    			$userdata = array (
    				'ID' => $user_id,
    				'first_name' => $fname,
    				'last_name' => $lname,
    				'nickname' => $username,
    				'user_nicename' => DIV_Helper::slugify($username)
    			);
    			wp_update_user( $userdata );
    
                      ...
    }

    Any clues as to how I can update the first & last names (not using some xprofile field that redundantly stores the same information), as well as any other custom fields I may add in the future. It’s seems I’m on the right track, this just is working right now.

    Thank youl

Viewing 1 replies (of 1 total)
  • @xtremefaith

    Participant

    Still working on this, I discovered something while debugging:

        1) If I set my priority to 1​ like so:

        add_action('bp_core_activated_user', array( $this, 'setup_registration_metadata'), 1 );

        then in my setup_registration_metadata() function I use add_user_meta() to capture the first_name & last_name values at that point in time. Thankfully it makes an entry in the usermeta table with the expected value from get_user_meta($user_id, 'first_name', true)

        2) Unfortunately, something else apparently is hooked afterwards (possibly a method from the “Enable BuddyPress to WordPress profile syncing”, which then overrides any value I had set for those fields so that in the end the WordPress fields are now incorrect or wiped.

    It seems apparent to me that the “Enable BuddyPress to WordPress profile syncing” option is the culprit, but would that be a bug that the setting is grabbing the wrong values because even if I don’t hook into bp_core_activated_user the meta values that were successfully stored at registration are then wiped during activation? For example:

    • first_name becomes the username
    • last_name is cleared completely

    Setting the priority to 20 the results show that by that point the originally stored meta_value(s) have already been wiped.

Viewing 1 replies (of 1 total)
  • The topic ‘Setting up activated users with stored metadata’ is closed to new replies.
Skip to toolbar