Skip to:
Content
Pages
Categories
Search
Top
Bottom

Add $_POST data before submitting registration


  • gorbett
    Participant

    @gorbett

    I have read a bunch of posts on registration (this one being closest to what I was looking for: https://buddypress.org/support/topic/modifying-customising-the-registration-process/) but have not found a good way to add $_POST data to the registration before BuddyPress starts processing it. I need a filter hook, but can’t find it.

    Essentially I want to combine two fields the user fills out, run a function to return a combined value, and pass that as $_POST data to BuddyPress.

    EXAMPLE: User fills out ‘First Name’ and ‘Last Name”. I want to combine those fields to create the Username after running a value from a function that will combine first and last name, check if the username_exists(), and possibly append a number to the end of it.

Viewing 9 replies - 1 through 9 (of 9 total)

  • coffeywebdev
    Participant

    @coffeywebdev

    I’m working on a solution for you now, will respond again soon.


    coffeywebdev
    Participant

    @coffeywebdev

    Copy the buddypress theme into your WordPress child theme, and then you can modify the register template if needed.

    copy the buddypress theme into your child theme by pasting this path into your child theme folder
    /wp-content/plugins/buddypress/bp-templates/bp-legacy/buddypress

    path of file for registration fields is this:
    /wp-content/themes/child-theme/buddypress/members/register.php

    Then, add First and Last name fields under User>Profile Links in the WordPress Dashboard.

    Currently there is a ‘Name’ field that is required, you can edit it to read ‘First Name’ then add another ‘Last Name’ field..

    Then you need to process the data before it saves into the database, I’m kind of at a loss here… I would need more time to brainstorm on it but I did find this hook ‘bp_core_signup_user’

    I believe this is the hook you are looking for:

    // define the bp_core_signup_user callback 
    function action_bp_core_signup_user( $array, $int, $int ) { 
        // make action magic happen here... 
    }; 
             
    // add the action 
    add_action( 'bp_core_signup_user', 'action_bp_core_signup_user', 10, 3 ); 

    Henry Wright
    Moderator

    @henrywright

    How about something like bp_signup_pre_validate?


    coffeywebdev
    Participant

    @coffeywebdev

    also ‘bp_signup_validate’

     function bp_password_beefing() {
     global $bp;
    
     if ( !empty( $_POST['signup_password'] ) )
       if ( strlen( $_POST['signup_password'] ) < 6 )
        $bp->signup->errors['signup_password'] = __( 'Your password needs to be at least 6 characters', 'buddypress' );  
     }
     add_action( 'bp_signup_validate', 'bp_password_beefing');

    source:
    http://wordpress.stackexchange.com/questions/160475/how-to-increase-password-requirements-for-registration


    gorbett
    Participant

    @gorbett

    thanks for all the help. I ended up doing something like this in my functions.php child theme:

    function assign_username() {
        if ( isset( $_POST ) ) {
            $_POST['signup_username'] = create_username();
        }
    }
    add_action( 'bp_signup_pre_validate', 'assign_username' );
    
    function create_username() {
        $i = 0;
        $username = $_POST['field_3'] . $_POST['field_4'];
         while ( username_exists( $username ) ) {
             $username = $username . ++$i;
         }
        return $username;
    }

    However, now I have some weird behavior that is asking me if I want to leave the page when submitting the form (typical window.onbeforeunload event when entering information: http://stackoverflow.com/questions/1119289/how-to-show-the-are-you-sure-you-want-to-navigate-away-from-this-page-when-ch) although I have no idea what is triggering it on my custom register.php (https://pantest.centralus.cloudapp.azure.com/register/).

    So I think my code above solves my $_POST issue, but now I have another one. Going to run through some tests to debug it now.

    Thanks again for the prompt help. Was hoping people still check out these forums.


    Henry Wright
    Moderator

    @henrywright

    If you comment out your action:

    // add_action( 'bp_signup_pre_validate', 'assign_username' );
    

    Do you still get the “Do you want to leave the page” message?

    I’m not sure why the code you’ve pasted above would trigger a message like that.


    gorbett
    Participant

    @gorbett

    Figured it out. Turns out that the button html element of the form was causing it. Fixed it by simply changing the button to <input type=”submit”>.

    Thanks again!


    gorbett
    Participant

    @gorbett

    Issue now is with the extra BP fields coming later in the DOM than my fields on my register.php. Even though I have the css set to display: none for the actual xprofile fields, my fields are not carrying through the $_POST values because display: none does not remove the fields from the DOM.

    I guess I can try adding hidden fields after the xprofile fields and set them to the values being entered in my fields, but that seems kinda clumsy.

    I added test code to check the $_POST values at where I can see the fields coming over as empty strings: https://pantest.centralus.cloudapp.azure.com/register/


    gorbett
    Participant

    @gorbett

    SOLUTION (finally)

    Thanks @henrywright and @coffeywebdev for pointing me in the right direction. Here is what I ended up with.
    1. Enable child theme and copy the BP register.php file to the child theme.
    2. In the child theme’s style.css, I entered to remove the xprofile fields from the register.php page:
    .editfield {
    display: none;
    }
    3. In the child theme’s functions.php file I have this (again, reassign the xprofile fields ‘field_3’ and ‘field_4’ because of their position in the DOM:

    add_action( ‘bp_signup_pre_validate’, ‘assign_username’ );

    function assign_username(){
    if ( isset( $_POST[‘first_name’] ) ) {
    $_POST[‘field_3’] = $_POST[‘first_name’];
    $_POST[‘field_4’] = $_POST[‘last_name’];
    $_POST[‘signup_username’] = create_username();
    foreach ($_POST as $key => $value) {
    echo $key . “: “;
    echo $value;
    echo “<br>”;
    }
    exit();
    }
    }

    function create_username() {
    $i = 0;
    $username = $_POST[‘field_3’] . $_POST[‘field_4’];
    while (username_exists( $username )) {
    $username = $username . ++$a;
    }
    return $username;
    }

    I am simply creating a username from first name and last name and then start to append a number when inevitably someone with the same name registers. I need to add more validation on the username to ensure that it is <= 50 characters, but this finally worked for me.

    Thanks again to the community for pointing me in the right direction!

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.
Skip to toolbar