Group Mods

  • Profile picture of @mercime
  • Profile picture of r-a-y
  • Profile picture of Hugo

Support: Creating & Extending

Existing and new plugins/components and themes.

How to add custom $usermeta to registration (16 posts)

Started 2 years, 7 months ago by: John James Jacoby

  • Profile picture of John James Jacoby John James Jacoby said 2 years, 7 months ago:

    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.

  • Yeah I think this is interesting. Is there a way to hack this to include a simple TOS button, one you need to click in order to register? I’ve tried to hack the TOS plugin, but could not make it the way I needed it.

  • Profile picture of John James Jacoby John James Jacoby said 2 years, 7 months ago:

    Same exact thing, except make the input a type=”checkbox” and give it the respective labels and things.

  • Profile picture of Jake Spurlock Jake Spurlock said 2 years, 7 months ago:

    Could this be used as a simple captcha? ie 2+2= “4″ and they have to put “4″. I have seen simple captchas like this where the field is labeled name or something, so the spammer put zuingling0234. Normal user just puts 4 and is able to proceed. Your thoughts?

  • Profile picture of David Lewis David Lewis said 2 years, 7 months ago:

    I’m gonna say no. This plugin is for adding metadata. Captcha and form validation is a different animal.

  • Profile picture of John James Jacoby John James Jacoby said 2 years, 7 months ago:

    @jake spurlock, if you want to write an evaluation function to check a value, that is possible starting with this process, but you would need to check the value and return an error message if it’s wrong. This example is just a very primitive textbox with no error checking to get started.

  • Profile picture of Travel-Junkie Travel-Junkie said 2 years, 7 months ago:

    To check if a field has been filled in you can use this function.

    function sv_check_tos()
    {
    	global $bp;
    	if ( empty( $_POST['name_of_field_in_template'] ) )
    		$bp->signup->errors['name_of_field_in_template'] = __( 'Your error message', 'sv' );
    }
    add_action( 'bp_signup_validate', 'sv_check_tos' );

    Useful for things like agreeing to TOS. Put the function in your functions.php or bp-custom.php file

  • Good one, thanks Travel-Junkie, would’ve been the next thing I’d have needed ;)

  • Profile picture of Travel-Junkie Travel-Junkie said 2 years, 7 months ago:

    Oh, forgot something. You also need to add
    do_action( 'bp_NAME_OF_FIELD_IN_TEMPLATE_errors' );
    where you want the error message to appear in your register.php file.

  • Profile picture of Travel-Junkie Travel-Junkie said 2 years, 7 months ago:

    Did some checking and it appears that the above code I posted doesn’t work as advertised.

    All the error messages are picked up correctly, but it seems that the additional $_POST values from register.php aren’t picked up, so it always displays an error message and a user basically can’t sign up.

    Any ideas are welcome, cause I don’t really have a clue :)

  • When you say add the following code to the register.php do you mean the wp-register.php file, the wp-signup.php, or the bp-core-signup.php file? When I open the wp-register.php file i see a comment that says this file is no longer used.

  • Profile picture of Travel-Junkie Travel-Junkie said 2 years, 6 months ago:

    I mean the registration/register.php file in your theme folder.

    Found out that the code does work, btw. Was something else that caused it to break.

  • Did you ever get the tos function to work? I’ve basically got the same problem which leads to displaying the error message, even when the tos field actually is ticked.

  • Excellent John James Jacoby!

    I was looking for a solution for this last Fall. You can see what I came up with here (How to synchronize firstname+lastname between xprofile field_1 and wp_usermeta upon registration). It’s part of a bunch of other stuff.

    But your code looks a lot more sophisticated. I’ll try to incorporate it into my upgrade to 1.1.3.

    Can something like this please become part of the core?

  • Profile picture of Mika Epstein (Ipstenu) Ipstenu said 1 year, 9 months ago:

    This only seems to half work.

    When an account registers, the metadata is stored in the signup table. When the account ACTIVATES, that data never shows up in the usermeta table.

    (I’m trying to record the IP address of user when they register and then spit it out in a pretty way. IMO, this is pretty important for CMS so you can see if a run of morons are all the same troll or not, rather than waiting for them to comment.)