Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to add custom $usermeta to registration

  • @johnjamesjacoby

    Keymaster

    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.

Viewing 18 replies - 1 through 18 (of 18 total)
  • @bpisimone

    Participant

    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.

    @johnjamesjacoby

    Keymaster

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

    @whyisjake

    Participant

    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?

    @takeo

    Participant

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

    @johnjamesjacoby

    Keymaster

    @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.

    @anonymized-96400

    Inactive

    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

    @bpisimone

    Participant

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

    @anonymized-96400

    Inactive

    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.

    @anonymized-96400

    Inactive

    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.

    @anonymized-96400

    Inactive

    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.

    @bpisimone

    Participant

    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.

    @peterverkooijen

    Participant

    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?

    @ipstenu

    Participant

    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.)

    @tomdebruin

    Participant

    Been messing around with adding custom usermeta data at signup and have come up with the following:

    `function bp_custom_user_signup() { ?>

    <?php
    }

    add_action(‘bp_before_registration_submit_buttons’, ‘bp_custom_user_signup’);

    // Add field_name from sign-up to usermeta on registration
    function bp_user_activate_field( $signup ) {

    update_usermeta( $signup, ‘name_of_field_in_template’, apply_filters( ‘get_name_of_field_in_template_value’, $_POST ) );

    return $signup;
    }
    add_filter( ‘user_register’, ‘bp_user_activate_field’ );
    `
    Note that this data is put into the database at the point of registration, not at user activation as the original example suggested – but I couldn’t get that to work.

    @manohark

    Participant

    Hi Everyone,

    Here is the solution on half part of the saving custom value in the user meta table:

    /* Add sign-up field to BuddyPress sign-up array */

    function bp_custom_user_signup_field( $usermeta ) {
    $usermeta[‘expertise’] = $_POST[‘expertise’];
    return $usermeta;
    }

    add_filter( ‘bp_signup_usermeta’, ‘bp_custom_user_signup_field’ );

    /* Add field_name from sign-up to usermeta on activation */
    function setMeta($user_id, $password, $meta)
    {
    global $wpdb;
    $signup_tbl = $wpdb->prefix.”signups”;
    $users_tbl = $wpdb->prefix.”users”;

    //Query to get the custom field store in the signup table here I have set it to expertise
    $metadata = $wpdb->get_var(“SELECT meta FROM “.$signup_tbl.” WHERE user_login=(SELECT user_login FROM $users_tbl WHERE ID=”.$user_id.”)”);

    $user_exprts = unserialize($metadata);
    update_usermeta( $user_id, ‘expertise’, $user_exprts[‘expertise’] );
    }
    add_action( ‘wpmu_activate_user’, ‘setMeta’, 10, 3);

    @chouf1

    Participant

    @imath provided another solution for single or multisite here 2 days ago after the same question on bp-fr.net

    https://gist.github.com/4310524

    @deepansrec

    Participant

    in this solution , is there any possibility to do dependent drop down,

    i need to store user meta data to created fields by x profile.

    like: xprofile field: field_1
    my custom file: replaces default x profile field_1 to custom field_1 and value must be stored in xprofile metadata

Viewing 18 replies - 1 through 18 (of 18 total)
  • The topic ‘How to add custom $usermeta to registration’ is closed to new replies.
Skip to toolbar