G
G
Grisha Nikolsky2015-12-26 20:47:27
WordPress
Grisha Nikolsky, 2015-12-26 20:47:27

Why is data update and redirect not working?

Hello. I wrote an HTML form + an input handler in PHP and for some reason this whole construction does not work. User data is not updated, and after clicking on the "Send" button, only the header is displayed, although the page is loaded with exactly the same address, that is: localhost/profile/?user=4, where the value of the user variable is the id of the owner of the personal account, which we consider. In this case, ours, because we are changing the data. I would be extremely grateful for your help.
HTML

<div id="edit" class="edit tab-pane fade" role="tabpanel">
                        <?php do_action( 'cgc_process_user_profile' ); ?>
                        <form method="POST">
                            <?php wp_nonce_field('user_profile_nonce', 'user_profile_nonce_field'); ?>
                            <div class="edit_block">
                                <label for="name">Изменить имя </label>
                                <input type="text" id="name" name="name" placeholder=" <?php echo $name; ?>">
                            </div>
                            <div class="edit_block">
                                <label for="lastname">Изменить фамилию </label>
                                <input type="text" id="lastname" name="lastname"
                                placeholder=" <?php echo $lastname; ?>">
                            </div>
                            <div class="edit_block">
                                <label for="nick">Изменить ник </label>
                                <input type="text" id="nick" name="nick" placeholder=" <?php echo $nick; ?>">
                            </div>
                            <div class="edit_block">
                                <label for="pass1">Изменить пароль </label>
                                <input type="password" id="pass1" name="pass1" placeholder=" ******">
                            </div>
                            <div class="edit_block">
                                <label for="pass2">Повторите пароль </label>
                                <input type="password" id="pass2" name="pass2" placeholder=" ******">
                            </div>
                            <button type="submit" class="btn btn-default"><?php echo 'Подтвердить' ?></button>
                        </form>
                    </div>

PHP
function cgc_process_user_profile_data() {

    if ( isset( $_POST['user_profile_nonce_field'] ) && wp_verify_nonce( $_POST['user_profile_nonce_field'], 'user_profile_nonce' ) ) {  //Проверка nonce_field

        $user_id = get_current_user_id();

        $user_data = array(
            'first_name' => sanitize_text_field( $_POST['name'] ),
            'last_name' => sanitize_text_field( $_POST['lastname'] ),
            'user_pass' => $_POST['pass1'],
        );

        if ( ! empty( $user_data['user_pass'] ) ) {

            if ( strcmp( $user_data['user_pass'], $_POST['pass2'] ) !== 0 ) {

                 wp_redirect('&password-error=false');

                exit;
            }

        } else {

            unset( $user_data['user_pass'] );
        }

        foreach ( $user_data as $key => $value ) {

            $results = '';
            if ( $key == 'user_pass' ) {

                wp_set_password( $user_data['user_pass'], $user_id );
                unset( $user_data['user_pass'] );

            } else {

                $results = wp_update_user( array( 'ID' => $user_id, $key => $value ) );

            }

            if ( ! is_wp_error( $results ) ) {

                wp_redirect('&profile-updated=true');

            }
        }

        wp_redirect('&profile-updated=false');

        exit;
    }
}
add_action( 'cgc_process_user_profile', 'cgc_process_user_profile_data' );

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Игорь Воротнёв, 2015-12-27
@VoxelGod

Функция определена, но нигде не вызывается. После определения функции надо добавить:

add_action( 'init', 'cgc_process_user_profile_data' );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question