BuddyDev

Search

[Resolved] Buddypress conditional registration

  • Keymaster
    (BuddyDev Team)
    Posts: 24642
    Brajesh Singh on #44787

    Hi Tosin,
    Thank you for the reply.

    Although it is fine to do that, I will rather write a function. If you check, there are 3 things which vary field id, value and error message.

    You can remove the previous code and use the following instead.

    
    
    // forces user to select a specific select field value on registration page.
    add_action( 'bp_signup_validate', function () {
    	bd_tosin_validate_signup_field( 4448, 'I accept', 'This is a required field. You must accept.' );
    
    	// You can uncomment the below function and specify your value. Repeat it as you need.
    	// bd_tosin_validate_signup_field( 2, 'some other value', 'Your error message' );
    	// bd_tosin_validate_signup_field( 2, 'some other value', 'Your error message' );
    
    } );
    
    /**
     * Validates BuddyPress signup field.
     *
     * Makes sure that the user entered value is same as the expected value. If not, the specified error will be shown.
     *
     * @param int $field_id
     * @param string $expected_value expected value to match.
     * @param string $error_message Error message to be shown if the values do not match.
     *
     * @return void
     */
    function bd_tosin_validate_signup_field( $field_id, $expected_value, $error_message = 'Please try again, the value did not match our expectation' ) {
    
    	if ( ! isset( $_POST[ 'field_' . $field_id ] ) ) {
    		return; //should never happen if the field is marked as required.
    	}
    
    	$input_value = wp_unslash( $_POST[ 'field_' . $field_id ] );
    
    	if ( strtolower( $expected_value ) !== strtolower( $input_value ) ) {
    		buddypress()->signup->errors[ 'field_' . $field_id ] = $error_message;
    	}
    }
    

    And you can use it for as many fields as you need.

    Regards
    Brajesh

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #44801

    Thanks Brajesh it worked perfectly

  • Keymaster
    (BuddyDev Team)
    Posts: 24642
    Brajesh Singh on #44812

    You are welcome.

The topic ‘ [Resolved] Buddypress conditional registration’ is closed to new replies.

This topic is: resolved