Search code examples
phpwordpressbuddypress

buddypress bp_core_add_message() doesn't fire why?


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 );

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.