Question

Ok I have the following php which work ok as in it run through to the IF but no matter what i do I can't get the bp_core message to fire on screen it just gives me the changes saved message... why what am i doing wrong!

function valid_postcode ($self) {
    $getFieldID      = $self->field_id;
    $PostCodeFieldID = 23;
    $postcodecheck   = $_POST['field_23'];

    if ( $getFieldID == $PostCodeFieldID || $postcodecheck == ''){

        $GetValuePost = $self->value;

        $regex = '/[a-z][0-9][a-z][- ]?[0-9][a-z][0-9]$/i';  

        if(!preg_match($regex, $GetValuePost)) {

            bp_core_add_message( __( 'That Postcode  is invalid. Check the formatting and try again.', 'buddypress' ), 'error' );

        }elseif (!isset($getFieldID)) {

            bp_core_add_message( __( 'You need to fill out the post code.', 'buddypress' ), 'error' );

        }
    }
}
add_action( 'xprofile_data_before_save', 'valid_postcode', 1, 1 );
Was it helpful?

Solution

You are hooking into xprofile_data_before_save. But xProfile component fires its own message after saving, so you should consider hooking into xprofile_screen_edit_profile to override default BuddyPress messages.

Though you will need to use global $_POST to get your data.

OTHER TIPS

OK this is what i ended up with to validate the custom profile field! this is a soft validation for canadian postal codes!

function valid_postcode ($data) {
        global $bp;

    $postcodecheck = $_POST['field_23'];
    $regex = '/[a-z][0-9][a-z][- ]?[0-9][a-z][0-9]$/i';  

    if(!preg_match($regex, $postcodecheck)) { 
        bp_core_add_message( __( 'That Postal code  is invalid. Check the formatting and try again.', 'buddypress' ), 'error' );
            wp_redirect( $bp->loggedin_user->domain . 'profile/edit/group/2/' );

        exit();

    }elseif ($postcodecheck == '') {

       bp_core_add_message( __( 'You need to fill out the Postal Code.', 'buddypress' ), 'error' );
            wp_redirect( $bp->loggedin_user->domain . 'profile/edit/group/2/' );

       exit();


    }
    return $data;
}

add_action( 'xprofile_data_before_save', 'valid_postcode');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top