Add confirm email field in register page

I share this trick with the wplms community.

If you want to add a confirm email field in the register form (run by BuddyPress signup), paste this code in your bp-custom.php file or in your custom theme function.php. I tested it in my function.php file and it works! :)

PHP code:

function registration_add_email_confirm(){ ?>
<?php do_action( 'bp_signup_email_first_errors' ); ?>
<input type="text" name="signup_email_first" id="signup_email_first" class="form_field" value="<?php
echo empty($_POST['signup_email_first'])?'':$_POST['signup_email_first']; ?>" />
<label>Confirm Email <?php _e( '(required)', 'buddypress' ); ?></label>
<?php do_action( 'bp_signup_email_second_errors' ); ?>
<?php }
add_action('bp_signup_email_errors', 'registration_add_email_confirm',20);

function registration_check_email_confirm(){
global $bp;

//buddypress check error in signup_email that is the second field, so we unset that error if any and check both email fields
unset($bp->signup->errors['signup_email']);

//check if email address is correct and set an error message for the first field if any
$account_details = bp_core_validate_user_signup( $_POST['signup_username'], $_POST['signup_email_first'] );
if ( !empty( $account_details['errors']->errors['user_email'] ) )
$bp->signup->errors['signup_email_first'] = $account_details['errors']->errors['user_email'][0];

//if first email field is not empty we check the second one
if (!empty( $_POST['signup_email_first'] ) ){
//first field not empty and second field empty
if(empty( $_POST['signup_email'] ))
$bp->signup->errors['signup_email_second'] = 'Please make sure you enter your email twice';
//both fields not empty but differents
elseif($_POST['signup_email'] != $_POST['signup_email_first'] )
$bp->signup->errors['signup_email_second'] = 'The emails you entered do not match.';
}
}
add_action('bp_signup_validate', 'registration_check_email_confirm');

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *