我正在尝试为“个人资料”下的“更改密码”创建自定义页面。当我存储/更新新用户密码(已更改为哈希值)时,它将自动注销。新密码可用于再次登录。有没有什么方法可以更新用户密码而无需注销?我想避免使用插件...下面是我的代码:-
<form method='post' action='changepassword'>
<div class='mypageMyDetailsBox'>
<span class='titleSub'>Password</span>
<table width='90%' align="center">
<tr>
<td width='40%'>Current Password</td>
<td width='60%'><input type='text' name='currentpassword' size='70'></td>
</tr>
<tr>
<td>New Password</td>
<td><input type='text' name='newpassword' size='70'></td>
</tr>
<tr>
<td>Confirm New Password</td>
<td><input type='text' name='confirmpassword' size='70'></td>
</tr>
</table>
</div>
</div>
<div align='center'>
<input type='submit' name='submit_update' value='Update' class='subUpt'>
</div>
</form>
<?php
if (isset($_POST['submit_update'])) {
$currentpassword = $_POST['currentpassword'];
$newpassword = $_POST['newpassword'];
require_once ABSPATH . 'wp-includes/class-phpass.php';
$wp_hasher = new PasswordHash( 8, true );
$user_info = get_userdata($currentUserID);
$user_pass = $user_info->user_pass;
if($wp_hasher->CheckPassword($currentpassword, $user_pass)) {
$passhash = wp_hash_password($newpassword);
$upd = $wpdb->query("UPDATE wp_users SET user_pass = '".$passhash."' WHERE ID = ".$currentUserID." LIMIT 1");
if ($upd) {
//Success
}
} else {
//Password not match
}
}
?>
提前谢谢您。
最佳答案
您应该尝试使用 wp_set_password ,而不是直接使用 WP_Query。虽然我没有专门测试它,但它应该会更新密码,并且不需要您注销并重新登录。
编辑:问题是 cookie 变得无效。您需要使用 wp_set_auth_cookie 设置/重置 cookie 。尝试添加这个:
if(!is_wp_error($update))
{
wp_cache_delete($user_ID,'users');
wp_cache_delete($user->user_login,'userlogins');
wp_logout();
if (wp_signon(array('user_login'=>$user->user_login,'user_password'=>$_POST['admin_pass1']),false)):
wp_redirect(admin_url());
endif;
ob_start();
}else{
wp_set_auth_cookie( $current_user_id, true);
}
关于php - WordPress - 自定义更改密码页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24602343/