How to add custom $usermeta to registration
-
Below is an example of how to add a simple text field to a new users usermeta when they register…
In a new plugin file, your functions.php, or bp-custom.php put…
/* Add sign-up field to BuddyPress sign-up array */
function bp_custom_user_signup_field( $usermeta ) {
$usermeta['field_name'] = $_POST['name_of_field_in_template'];
return $usermeta;
}
add_filter( 'bp_signup_usermeta', 'bp_custom_user_signup_field' );
/* Add field_name from sign-up to usermeta on activation */
function bp_user_activate_field( $signup ) {
update_usermeta( $signup['user_id'], 'field_name', $signup['meta']['name_of_field_in_template'] );
return $signup;
}
add_filter( 'bp_core_activate_account', 'bp_user_activate_field' );Then somewhere in your custom register.php file, put…
<input type="input" name="name_of_field_in_template" id="name_of_field_in_template" value="" />
Replace “name_of_field_in_template” and “field_name” with what you need, and rename the functions if you want to, etc… Now when people sign up on your website, you can have custom usermeta that gets entered when they activate their account!
If you wanted to pump this up a notch, it’s possible to create a function to retain the value and send/receive field formatting errors also, so you can expand this to do something like youtube or twitter id’s on sign-up, outside of tying the fields into xprofile. Obvious uses for this would be to integrate BP with existing plugins that use usermeta already, letting your sign-up process get the headstart on setting up user accounts with the information those plugins want/need.
Have fun! Happy hacking!
I might make an example plugin for people to tear apart later if there’s interest.
- The topic ‘How to add custom $usermeta to registration’ is closed to new replies.