我正在前端处理自己的帐户管理。到目前为止,一切都很顺利。我有一个问题似乎无法解决。
当用户更新其帐户信息时,它会提交给自己并执行保存逻辑。页面加载时,除电子邮件外,用户详细信息显示为更新值。如果我重新加载页面,将显示正确的电子邮件。
这是更新详细信息的方法:
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\'));
}
}
表格为:
<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>
请注意
$current_user
是全球WordPress。
即使密码没有更改,电子邮件也是错误的。我假设这是由于autologin解决了强制注销问题(不需要关于良好实践的讲座,这里不是我的电话)
我假设我丢失了一些缓存删除,但我似乎不知道应该是什么。
谢谢你在这方面的帮助。