Skip to:
Content
Pages
Categories
Search
Top
Bottom

Bypass Email Requirement


  • metalhead505
    Participant

    @metalhead505

    This is a subject that’s been talked about off and on in Buddypress history. We all know that it’s handy to have a user’s email address for many reasons, but in general, people prefer to be asked less questions when signing up at a new website.

    I hired someone for help with this a couple years ago, and the code they gave me still works (only with the legacy template pack though!) Here’s the code:

    /**
     * Generate Random, unused email from the username
     *
     * @param string $username
     *
     * @return string random email
     */
    function hello_generate_random_email( $username ) {
    	$domain = '@metalheadswebsite.com';
    	$username = sanitize_user( $username );
    	$email = $username . $domain;
    
    	while( email_exists( $email ) ) {
    		//try another random email by appending some digit
    		$email = $username . random_int( 1, 99999 ) . $domain;// there is vary low chance of having 100K users with same name anyway
    	}
    
    	return $email;
    }
    function hello_updatre_email() {
    	// If the username is not given, let us not worry much
    	if ( empty( $_POST['signup_username'] ) ) {
    		return ;
    	}
    
    	//Also, if an email is already given by the user, do not generate one
    
    	if ( ! empty( $_POST['signup_email'] ) ) {
    		return ;
    	}
    
    	//now generate a random email and save it
    	$email = hello_generate_random_email( $_POST['signup_username'] );
    
    	$_POST['signup_email'] = $email;
    
    }
    add_action( 'bp_signup_pre_validate', 'hello_updatre_email' );

    This code works perfectly with the legacy template pack. The user signs up, and the function takes the given username & appends @metalheadswebsite.com to it, and submits it with the form. I’ve been using this code since 2017.

    My problem is that now I want to switch to the Nouveau template pack, but this code does not work with it; it fails; users are prompted for the email address after hitting “Submit.”

    I don’t understand how the nouveau registration works; the markup in that file is harder for me to understand than the one in legacy. Is it something simple that I need to do? Does anything stand out based on the code I provided?

    PS) As I mentioned, I know this has been talked about in the past. I’ve read the suggestions people have given, but those suggestions are dusty now; the plugin called “Optional Email” does not work with Buddypress (Nouveau), at least not as of today.

    The person who created the custom code for me is no longer doing this type of work. ­čÖü

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

  • Venutius
    Moderator

    @venutius

    Hi there,

    This is happening because the Nouveau register page will not submit without an email being entered into the field. There’s a number of possible solutions to this, all of them pretty hacky. So for example you could use JavaScript to remove the required status of the email field. However you mentioned overloading the register page and I’ll give you my more hacky solution. Be warned you will probably need to do more work to get your register page looking right. My solution is to replace that section of the Nouveau registration form with the Legacy content.

    So you’d replace this:

    <?php bp_nouveau_signup_form(); ?>

    with this:

    <label for="signup_username"><?php _e( 'Username', 'buddypress' ); ?> <?php _e( '(required)', 'buddypress' ); ?></label>
    
    <input type="text" name="signup_username" id="signup_username" value="<?php bp_signup_username_value(); ?>" <?php bp_form_field_attributes( 'username' ); ?>/>
    
    <label for="signup_email"><?php _e( 'Email Address', 'buddypress' ); ?> <?php _e( '(required)', 'buddypress' ); ?></label>
    
    <input type="email" name="signup_email" id="signup_email" value="<?php bp_signup_email_value(); ?>" <?php bp_form_field_attributes( 'email' ); ?>/>
    
    <label for="signup_password"><?php _e( 'Choose a Password', 'buddypress' ); ?> <?php _e( '(required)', 'buddypress' ); ?></label>
    
    <input type="password" name="signup_password" id="signup_password" value="" class="password-entry" <?php bp_form_field_attributes( 'password' ); ?>/>
    
    <div id="pass-strength-result"></div>
    
    <label for="signup_password_confirm"><?php _e( 'Confirm Password', 'buddypress' ); ?> <?php _e( '(required)', 'buddypress' ); ?></label>
    
    <input type="password" name="signup_password_confirm" id="signup_password_confirm" value="" class="password-entry-confirm" <?php bp_form_field_attributes( 'password' ); ?>/>

    Like I say it’s not the prettiest solution but it should get you moving forward.


    Venutius
    Moderator

    @venutius

    Hi there,

    I’ve come up with a better way of doing this.

    If you want to remove the email field altogether, you can add this to your functions.php:

    add_action( 'bp_nouveau_get_signup_fields', 'venutius_remove_email_signup_field' );
    
    function venutius_remove_email_signup_field( $fields ) {
    	
    	unset( $fields['account_details']['signup_email'] );
    	
    	return $fields;
    }

    alternatively if you want to retain the field but just want to remove the required attribute you can add this:

    add_action( 'bp_nouveau_get_signup_fields', 'venutius_remove_email_signup_field_required' );
    
    function venutius_remove_email_signup_field_required( $fields ) {
    	
    	$fields['account_details']['signup_email']['required'] = false;
    	
    	return $fields;
    }

    metalhead
    Participant

    @aaronthomas1979

    Thank you! I appreciate your help with this. Works great. Now I can start enjoying all those nifty Nouveau enhancements.


    noestoyfino
    Participant

    @noestoyfino

    Hola,

    Cuando pongo este código, me quita el campo email, pero a la hora de registrar un usuario, no me lo registra. Me vuelve a la misma página y me señala en rojo la contraseña. He probado con varias contraseñas. A que puede ser debido?


    Venutius
    Moderator

    @venutius

    That’s because this was a solution for a specific issue, one where the user already had code to input to get around the fact that the email is needed for the form validation. Removing the email field is not enough, you’ve got to input a pseudo email into the data before the form is validated.


    beekoff
    Participant

    @beekoff

    Hi Venutius – Chrome was returning the following errors, and thus I’ve added each of those fields into your snippet (at bottom). And unfortunately it’s still not working (resulting Chrome errors are identical).

    An invalid form control with name=’field_2′ is not focusable.
    (index):1 An invalid form control with name=’field_15′ is not focusable.
    (index):1 An invalid form control with name=’field_4′ is not focusable.
    (index):1 An invalid form control with name=’field_6′ is not focusable.
    (index):1 An invalid form control with name=’field_13′ is not focusable.
    (index):1 An invalid form control with name=’field_7′ is not focusable.
    (index):1 Unchecked runtime.lastError: The message port closed before a response was received.

    Here’s what I added to my theme’s functions.php (and I am also using template Nouveau, btw):

    add_action( 'bp_nouveau_get_signup_fields', 'venutius_remove_email_signup_field_required' );
    
    function venutius_remove_email_signup_field_required( $fields ) {
    	
    	$fields['field_2']['field_15']['field_4']['field_6']['field_13']['field_7']['required'] = false;
    	
    	return $fields;
    }

    The submit button is still seeing the conditionally hidden required fields on my registration form (which are blank), and not letting the form to submit.

    Do you see any issues with my customization to your snippet?

    Thanks,
    Sam

    //Sorry if this is a duplicate post


    Venutius
    Moderator

    @venutius

    Not sure if this will work but your syntax is clearly wrong, the correctway to write it would be:

    add_action( 'bp_nouveau_get_signup_fields', 'venutius_remove_email_signup_field_required' );
    
    function venutius_remove_email_signup_field_required( $fields ) {
        $custom_fields = array('field_2', 'field_15', 'field_4', 'field_6','field_13', 'field_7');
        foreach ( $custom_fields as $field ) {
            $fields[$field]['required'] = false;
        }
    	return $fields;
    }

    beekoff
    Participant

    @beekoff

    Darn-it. Not yet. Will keep trying little tweaks.

    Thanks for your help, regardless, Venutius.

    Sam


    Venutius
    Moderator

    @venutius

    Yep tht function works for the main WordPress signup fields, not the extended profile fields. However, thinking about it what this would do would make all those fields options, so why don’t you make all the fiedls optional in your settings, then you would not get the error, obviously it means making fields you want filling out optional but the way Nouveau works there’s not really an alternative if you are going to hide them for some member types.

    The alternative would be to create a multi-stage signup form where the first page takes in the basic signup information and member type then the second page displays the extended profile info specific to the member type.

    BP Better Registration splits the form into sections, I wonder if that could be used to create such a signup form?


    beekoff
    Participant

    @beekoff

    Thanks for your suggestions @venutius. With these the only options I’m going to wait for BuddyDev to update a workaround into BuddyPress Member Types Pro plugin (he’s very proactive, and says it’s now in queue).

    Hopefully Nouveau and future BP templates will make conditionally hidden (but required) xProfile fields inert in this state. These fields are so important to augmenting registration on BP sites, and so hopefully it gets baked into the code (instead of having to find workarounds).

    I appreciate your help!

    Sam

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