Setting up activated users with stored metadata
-
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
- The topic ‘Setting up activated users with stored metadata’ is closed to new replies.