0

I am trying to validate the Custom Field phone using user_profile_update_errors hook but edit_user_profile_update and personal_options_update run before it as stated here https://codex.wordpress.org/Plugin_API/Action_Reference/user_profile_update_errors I tried using a workaround by hooking save_profile_fields only if $errors is empty but the function doesn't hook

function init() {
    //render profile fields
    add_action( 'show_user_profile', array( $this, 'render_profile_fields' ) );
    add_action( 'edit_user_profile', array( $this, 'render_profile_fields' ) );
    // validate profile fields
    add_action( 'user_profile_update_errors', array( $this, 'validate_profile_phone' ), 0, 3 );
    add_action( 'user_profile_update_errors', array( $this, 'validate_profile_address' ), 0, 3 );

}

function render_profile_fields( $user ) { ?>

    <h3>More Information</h3>
    <table class="form-table">
        <tr>
            <th><label for="phone">Phone</label></th>
            <td>
                <input type="tel" name="phone" id="phone" value="<?php echo esc_attr( get_the_author_meta( 'phone', $user->ID ) ); ?>" class="regular-text" ><br>
            </td>
        </tr>            
    </table>

<?php }

function validate_profile_phone( $errors, $update, $user ) {
    $error_check = false;
    $phone_regex = "/08[789]\d{7}/u";
// validate input fields
    if ( !empty( $_POST['phone'] ) && strlen( $_POST['phone'] ) > 10) {
        $errors->add( 'phone', "<strong>ERROR</strong>: The maximum phone length is 10 characters." );
        $error_check = true;
    }

    if ( preg_match( $phone_regex, $_POST['phone'] ) == 0 ) {
        $errors->add( 'phone', "<strong>ERROR</strong>: Not a valid phone number." );
        $error_check = true;
    }
    
    if( $error_check ) {
        return $errors;
    }

    add_action( 'edit_user_profile_update', array( $this, 'save_profile_fields' ) );
    add_action( 'personal_options_update', array( $this, 'save_profile_fields' ) );

}

function save_profile_fields( $id ) {
//  save input fields values
    if ( !current_user_can( 'edit_user', $id ) )
        return false;

    if ( isset( $_POST['phone'] ) )
        update_user_meta( $id, 'phone', $_POST['phone'] );
}
PGMatev
  • 5
  • 3
  • The hooks you use are dependant on where the funtion/output is occurring. The use of `edit_user_profile_update` and `personal_options_update` are for backend admin. Secondly you can do all your validation in one function. I asked this question only a couple of days ago. You can view the thread here and the answer provided solved my issue. I will attempt to provide you a more detailed answer below shortly, but until then please have a look at: https://stackoverflow.com/questions/62813524/save-filtered-billing-phone-to-admin-user-profile-in-woocommerce – Aliqua Jul 11 '20 at 05:51

1 Answers1

0

The user_profile_update_errors hook fires before user profile update errors are returned. You can create separate functions for your errors/validation to your other actions and you can save all your custom fields (eg: phone, address, etc) and validate all your custom fields (eg: phone, address, etc) in one function each.

// validate profile field
add_action( 'user_profile_update_errors', array( $this, 'validate_profile_fields' ), 0, 3 );

// save profile fields to your own profile and others.
add_action( 'edit_user_profile_update', array( $this, 'save_profile_fields' ) );
add_action( 'personal_options_update', array( $this, 'save_profile_fields' ) );
    
function validate_profile_fields( &$errors, $update, &$user ) {
    $phone_regex = "/08[789]\d{7}/u";
    
    if ( !empty( $_POST['phone'] ) && strlen( $_POST['phone'] ) > 10) {
      
        $errors->add( 'phone', "<strong>ERROR</strong>: The maximum phone length is 10 characters." );

    }
    if ( preg_match( $phone_regex, $_POST['phone'] ) == 0 ) {
        
        $errors->add( 'phone', "<strong>ERROR</strong>: Not a valid phone number." );
    }
    
    return $errors;

}

function save_profile_fields( $id ) {
    
    // save input fields values
    $phone_regex = "/08[789]\d{7}/u";
    
    if ( !current_user_can( 'edit_user', $id ) )
        return false;

    if ( isset( $_POST['phone'] )) {

        if ( preg_match( $phone_regex, $_POST['phone'] ) == 1 ) {
        update_user_meta( $id, 'phone', sanitize_text_field( $_POST['phone'] ) );
}

From what I am understanding, you don't have to do all the same validations in the save function, as they have been set in the validation/errors function - so if they all pass in the errors function, then the save function fires.

Goodluck, and happy coding.

In addition, don't forget to prefix your functions with something unique to ensure that they will not conflict with other functions in other themes/plugins. You can use random characters, or an abbreviation of your plugin, eg: function abc1234_save_profile_fields() or whatever you like.

If my answer is satisfactory, please mark it as your answer.

Aliqua
  • 723
  • 7
  • 21