buddypress bp_core_add_message()不解雇原因?

时间:2014-05-07 18:12:22

标签: php wordpress buddypress

好的我有以下的PHP工作正常,因为它贯穿到IF但无论我做什么我都无法在屏幕上触发bp_core消息它只是给我更改保存的消息... 为什么我做错了!

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

2 个答案:

答案 0 :(得分:1)

您正在加入xprofile_data_before_save。但是xProfile组件在保存后会触发自己的消息,因此您应该考虑挂钩xprofile_screen_edit_profile以覆盖默认的BuddyPress消息。

虽然您需要使用全局$ _POST来获取数据。

答案 1 :(得分:0)

好的,这就是我最终验证自定义配置文件字段的原因!这是对加拿大邮政编码的软性验证!

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