Buddypress signup form hook

DescriptionBuddypress snippet by - 07/25/10

Alright, this is a result of about 5 hours worth of experimentation with the buddypress registration form.

This code can be added to a plugin or to your functions.php file. This hooks into the default sign up form and allows you to add an additional field. The bp_signup_validate action allows you to add an error to the $bp superglobal in the signup object. This example checks to see if the post variable is empty or not and then adds an error to the superglobal if not. You can add as much functionality as needed in bph_check_validation function.

The second action is one of the hooks within the register.php file that resides in the bp-default theme. You can choose from any of the do_action hooks to place the html where you want in that form.

Tags

<?php
function bph_field_value() {
	echo get_bph_field_value();
}
	function get_bph_field_value() {
		return apply_filters( 'get_bph_field_value', $_POST['bph_field'] );
	}
	
function bph_check_validation(){	
	global $bp;
	$bph_field_txt = $_POST['bph_field'];
	
	if (empty($bph_field_txt) || $bph_field_txt == '') {
		$bp->signup->errors['bph_field'] = __('This is a required field','buddypress');
	}
	return;
}

function bph_show_input_field(){?>
	<div style="float:right;width:268px;margin-right:4px;" class="bph_field_container">
    <label>Input Field Name</label>
    <?php do_action( 'bp_bph_field_errors' ) ?>
    <input type="text" name="bph_field" id="bph_field" value="<?php bph_field_value() ?>" /><br />
    
	</div><?php
}

add_action('bp_signup_validate', 'bph_check_validation');
add_action('bp_before_registration_submit_buttons', 'bph_show_input_field');
?>
  • Share
Authored by: Adam J Nowak
http://hyperspatial.com

4 Responses to “Buddypress signup form hook”

  1. Ok, now that I've added field to the signup form, how can I use it?

    One option is to add whatever code I want to execute in function called by bp_signup_validate, but there is no guarantee that if my custom field validates some other field will not give error. So, where should I enter code that I want to execute against successful signup (using data from custom field that we just added)?

  2. I have only used this code for validation, the buddypress humanity plugin I wrote just checks to see if you filled out the form with the right answer.

    From within your plugin / bp_signup_validate, you could probably just save the value of the input field to a wordpress option or to the database. Something like: add_option('myNewValue',$_POST['bph_field']);

    Then do whatever you want with that data.

  3. Hello Adam,
    Thanks for the codes! I have 2 question need your help.
    I don't want to print a error message, can I just remove this line?
    ''

    Another question- Instead the error message, I prefer change the label color to red upon error. I wrote a code for this purpose.
    'function print_style_color() {
    global $bp;
    $errname = 'bp_' .bp_the_profile_field_input_name( ). '_errors';
    if ( !empty( $errname[echo $fieldname] ) )
    echo 'color:red';
    }
    add_action ( 'print_style_color','print_style_color' );'
    For other bp fields, it works with 'if ( !empty( $bp->signup->errors['signup_password'] ) )' , but for bp-humanity, this $bp_signup_erro doesn't work. So, I try to use 'if ( !empty( $errname[echo $fieldname] ) )' , it doesn't work either.
    Would you suggest me what's wrong in my code?

  4. hi Adam,
    thanks for the useful snippet, I used it in my next BP theme.
    worked like a charm ;)

Comments and Feedback