I have the following code in my plugin, and the error validation works perfectly and displays at the top of the user-edit page, however everything that I have tried with relation to saving... the conditionals are ignored. If I leave it empty, it displays the appropriate error but still updates the $billing_field
with '', if I have 20 characters in there, or a bunch of letters, the error comes up, but it still updates it no matter what the $_REQUEST['billing_phone'] / $billing_phone
value is... it feels as though the entire edit_user_profile_update
and personal_options_update
is being ignored?
public function audp_validate_user_account_fields( &$errors, $update = null, &$user ) {
// Billing Phone
$billing_phone = str_replace( array( ' ', '(', ')' ), '', $_REQUEST['billing_phone'] );
if ( !empty( $_REQUEST['billing_phone'] ) ) {
if ( !preg_match( '/^04[0-9]{8}$/D', $billing_phone ) ) {
// Invalid Format
$errors->add( 'billing_phone', __( 'Mobile Phone is invalid. Eg: 0412 345 678' ) );
}
$billing_phone_query = get_users( array(
'meta_key' => 'billing_phone',
'meta_value' => $billing_phone,
) );
foreach ( $billing_phone_query as $query ) {
if ( $user->ID != $query->ID ) {
$errors->add( 'billing_phone', __( 'Mobile Phone already exists.' ) );
}
}
} else {
// Empty Field
$errors->add( 'billing_phone', __( 'Mobile Phone Field Required.' ) );
}
}
public function audp_save_user_account_fields( $user_id ) {
// Billing Phone
$billing_phone = str_replace( array( ' ', '(', ')' ), '', $_REQUEST['billing_phone'] );
if ( !empty( $_REQUEST['billing_phone'] ) ) {
if ( preg_match( '/^04[0-9]{8}$/D', $billing_phone ) ) {
$billing_phone_query = get_users( array(
'meta_key' => 'billing_phone',
'meta_value' => $billing_phone,
) );
foreach ( $billing_phone_query as $query ) {
if ( $user_id == $query->ID ) {
update_user_meta( $user->ID, 'billing_phone', $billing_phone );
} else {
return false;
}
}
} else {
return false;
}
} else {
return false;
}
$this->loader->add_action( 'personal_options_update', $plugin_admin, 'audp_save_user_account_fields' );
$this->loader->add_action( 'edit_user_profile_update', $plugin_admin, 'audp_save_user_account_fields' );
$this->loader->add_action( 'user_profile_update_errors', $plugin_admin, 'audp_validate_user_account_fields', 10, 3 );
Aucun commentaire:
Enregistrer un commentaire