• Days and days spent trying to solve this mistake but seems to be no solution…
    In my function.php I add some user extra fields:

    function add_additional_user_meta( $profileuser ) {
       // here my input fields
       <input type="text" id="test" name="test" value="<?php echo esc_attr( $profileuser->test ); ?>" />
    }
    add_action('show_user_profile', 'add_additional_user_meta');
    add_action('edit_user_profile', 'add_additional_user_meta');

    Than I add the action hook to save them:

    function save_additional_user_meta( $user_id ) {
       // here I save fields
       update_user_meta( $user_id, 'test', $_POST['test'] );
    }
    add_action( 'personal_options_update', 'save_additional_user_meta' );
    add_action( 'edit_user_profile_update', 'save_additional_user_meta' );

    Ok! That’s work!
    But.. What about validation?

    I add another action hook for validate and display error message

    function check_fields($errors, $update, $user){
       // here make some validation
       if ( empty( $_POST['test'] ) ) {
    $errors->add( 'empty_test', __( '<strong>ERROR</strong>: You must write the test field' ) );
       }
    }
    add_filter('user_profile_update_errors', 'check_fields', 10, 3);

    The error message work but the field is saved however!
    Where is the mistake?

    Hope someone can help me!
    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • I’ve just tested out your code on a localhost installation, and the error message doesn’t ever display for me. The fields get saved regardless of what is entered into the field, or if it’s left blank. No error ever displays for me.

    That may be half the problem.

    It would be better to use ! isset( $_POST['test'] ) instead of empty( $_POST['test'] ) because with the empty() function it will return true if the variable is an empty string, false, array(), NULL, “0?, 0, and an unset variable.

    Thread Starter Banilla87

    (@banilla87)

    Thanks but the field is always saved and no error is returned!
    I show you that code just for example. My original one is:

    // ADD FIELD
    function add_additional_user_meta( $profileuser ) {
    
       // here my input fields
       <input type="text" size="20" name="user_pec" id="user_pec" value="<?php echo esc_attr( $profileuser->user_pec ); ?>" />
    
    }
    add_action('show_user_profile', 'add_additional_user_meta');
    add_action('edit_user_profile', 'add_additional_user_meta');
    
    // SAVE FIELD
    function save_additional_user_meta( $user_id ) {
       // here I save fields
       update_user_meta( $user_id, 'user_pec', sanitize_text_field( $_POST['user_pec'] ) );
    }
    add_action( 'personal_options_update', 'save_additional_user_meta' );
    add_action( 'edit_user_profile_update', 'save_additional_user_meta' );
    
    // CHECK FIELD
    function check_fields($errors, $update, $user){
       // here make some validation
        if ( ! isset( $_POST['user_pec'] ) ) {
            $errors->add( 'empty_user_pec', __( '<strong>ERROR</strong>: Insert e-mail PEC.' ), array( 'form-field' => 'user_pec' ) );
        } elseif ( !is_email( $_POST['user_pec'] ) ) {
            $errors->add( 'invalid_user_pec', __( '<strong>ERROR</strong>: not valid e-mail PEC' ), array( 'form-field' => 'user_pec' ) );
        } elseif ( ( $owner_id = email_exists($_POST['user_pec']) ) && ( !$update || ( $owner_id != $user->ID ) ) ) {
            $errors->add( 'user_pec_exists', __('<strong>ERROR</strong>: Already exist'), array( 'form-field' => 'user_pec' ) );
        }
       }
    }
    add_filter('user_profile_update_errors', 'check_fields', 10, 3);

    Last function seems not work!
    If I put a number inside the field, error appears but the field is saved however!

    Any idea?

    Thread Starter Banilla87

    (@banilla87)

    Probably user_profile_update_errors not work with wordpress core?

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Validate extra user fields in profile page’ is closed to new replies.