0

I'm working on my own account management from the frontend. So far everything is working pretty good. I do have 1 issue that I just can't seem to fix.

When a user updates their account information, it submits to itself and the save logic is performed. When the page loads, the user details show as the updated values with the exception of the email. If I reload the page, the correct email is shown.

This is the method to update the details:

private function _updateDetails() {
    global $current_user;
    $nonce = !empty($_POST['_wpnonce']) ? $_POST['_wpnonce'] : '';
    if ( !$nonce || !wp_verify_nonce( $nonce, 'update-details_' . $this->_userID ) ) {
        throw new Exception(__('Form validation failed, please refresh the page and try again.', 'parables_membership'));
    } else {
        $saveUserData = array();
        if (!empty($_POST['first_name']) && $_POST['first_name'] != $current_user->get('first_name')) {
            $saveUserData['first_name'] = $_POST['first_name'];
        }
        if (!empty($_POST['last_name']) && $_POST['last_name'] != $current_user->get('last_name')) {
            $saveUserData['last_name'] = $_POST['last_name'];
        }
        if (!empty($_POST['email']) && $_POST['email'] != $current_user->get('user_email') && !email_exists( $_POST['email'] )) {
            $saveUserData['user_email'] = $_POST['email'];
        }
        // See if we should update the password
        if (!empty($_POST['pass1']) && !empty($_POST['pass2']) && $_POST['pass1'] === $_POST['pass2']) {
            wp_set_password( $_POST['pass1'], $this->_userID );
            $this->_passwordUpdated = true;
        }
        // Save data
        if (!empty($saveUserData)) {
            $saveUserData['ID'] = $this->_userID;
            $result = wp_update_user($saveUserData);
            if (!is_wp_error($result) && $result > 0) {
                $this->_updated = true;
            }
            // Fix auto logout when updating password
            if ($this->_passwordUpdated) {
                $userLogin = $current_user->get('user_login');
                wp_cache_delete($this->_userID, 'users');
                wp_logout();
                wp_signon(array('user_login' => $userLogin, 'user_password' => $_POST['pass1']));
            }
            return $result;
        }
        throw new Exception(__('No details were modified', 'parables_membership'));
    }
}

The form is:

<form id="parables-membership" action="<?php echo get_permalink(); ?>" method="post" data-step="details">
    <?php wp_nonce_field( 'update-details_' . $user_id ) ?>
    <input type="hidden" name="action" value="update">
    <input type="hidden" name="user_id" id="user_id" value="<?php echo esc_attr( $user_id ); ?>">
    <input type="hidden" name="admin_bar_front" value="0">
    <input type="hidden" name="rich_editing" value="<?php echo esc_attr( get_user_meta( $user_id, 'rich_editing', true ) ) ?>">
    <input type="hidden" name="admin_color" value="<?php echo esc_attr( get_user_meta( $user_id, 'admin_color', true ) ) ?>">
    <input type="hidden" name="comment_shortcuts" value="<?php echo esc_attr( get_user_meta( $user_id, 'comment_shortcuts', true ) ) ?>">

    <fieldset>
        <legend><?php _e( 'Edit your details', 'parables' ) ?></legend>

        <div class="form-element">
            <label class="control-label" for="first_name"><?php _e( 'First Name', 'parables' ); ?></label>
            <div class="element">
                <input type="text" class="input-xlarge" id="first_name" name="first_name" placeholder="" value="<?php esc_attr_e($current_user->first_name); ?>" >
            </div>
        </div>

        <div class="form-element">
            <label class="control-label" for="last_name"><?php _e( 'Last Name', 'parables' ); ?></label>
            <div class="element">
                <input type="text" class="input-xlarge" id="last_name" name="last_name" placeholder="" value="<?php esc_attr_e($current_user->last_name) ?>" >
            </div>
        </div>

        <div class="form-element">
            <label class="control-label" for="email"><?php _e( 'Email', 'parables' ); ?></label>
            <div class="element">
                <input type="text" class="input-xlarge" name="email" id="email" value="<?php esc_attr_e($current_user->user_email) ?>" />
            </div>
        </div>

        <hr/>

        <div class="form-element">
            <p class="help-block"><?php _e( 'To change your password, enter the new password below and then repeat it to confirm, otherwise leave these two fields blank.', 'parables' ); ?></p>
        </div>

        <div class="form-element">
            <label class="control-label" for="pass1"><?php _e( 'New Password', 'parables' ); ?></label>
            <div class="element">
                <input type="password" class="input-xlarge" name="pass1" id="pass1" value="" autocomplete="off">
            </div>
        </div>

        <div class="form-element">
            <label class="control-label" for="pass1"><?php _e( 'Confirm Password', 'parables' ); ?></label>
            <div class="element">
                <input type="password" class="input-xlarge" name="pass2" id="pass2" value="" autocomplete="off">
            </div>
        </div>

        <p><input type="submit" value="<?php _e( 'Update Account', 'parables' ); ?>" class="alignright button button-primary <?php echo apply_filters( 'membership_account_button_color', '' ); ?>" name="submit"></p>
    </fieldset>
</form>

Note that $current_user is the WordPress global.

The email is wrong even if the password is not altered. I was assuming it was due to the autologin to solve the forced logout issue (don't need a lecture in good practices, not my call here)

I am assuming I am missing some cache delete but I can't seem to figure out what it should be.

Thanks for any assistance in this.

3
  • Have you tried putting this at the end of your function: wp_set_current_user( $user_id ); (You'd need to get the current user id into the $user_id var.) I can't honestly say I know why it works but this did solve a similar issue for me. Dec 19, 2014 at 23:31
  • Thanks for the suggestion, unfortunately this didn't change anything. Very interesting.
    – Jeremy
    Dec 23, 2014 at 16:20
  • I was able to solve this by doing: ` $current_user->data->user_email = $_POST['email']; // fix for showing wrong email on save `
    – Jeremy
    Dec 23, 2014 at 16:37

1 Answer 1

0

Since the issue was only on the initial page load after submission, I was able to solve this by doing a dirty hack.

$current_user->data->user_email = $_POST['email']; // fix for showing wrong email on save

This solved my issue for my needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.