我创建了一个简单的表单,让我的用户可以更改他们的密码。但有一个问题,我很困惑!我尝试了很多次来更改密码,但我的密码不会被更改wp_set_password()
我真的不知道原因。
<?php /* Template Name: user-edit-password */ ?>
<?php
$user = wp_get_current_user();
$userID = $user->ID;
$has_error = false;
$has_success = false;
$message = array();
if( isset($_POST[\'karneta_pass_submit\']) ){
if( !isset($_POST[\'security\']) || !wp_verify_nonce($_POST[\'security\'],\'edit-profile-password-nonce\') ){
print(\'do not damage that\');
} else {
$currentpass = sanitize_text_field($_POST[\'karneta_currentpass\']);
$newpass = sanitize_text_field($_POST[\'karneta_newpass\']);
$repeatnewpass = sanitize_text_field($_POST[\'karneta_repeatnewpass\']);
if( wp_check_password($currentpass, $user->data->user_pass, $UserID) ){
if( empty($currentpass) || empty($newpass) || empty($repeatnewpass) ){
$has_error = true;
$message[] = "fill all the fields";
}
elseif( $newpass !== $repeatnewpass ){
$has_error = true;
$message[] = "they are not the same";
}
else {
wp_set_password($newpass,$UserID);
$has_success = true;
$message[] = "password changed successfully";
}
} else {
$has_error = true;
$message[] = "the old password is not correct";
}
}
}
?>
<div class="usereditprofile">
<div class="usereditprofilediv">
<div>
<?php if( $has_error ){ ?>
<div class="userprofile_message error">
<?php foreach ($message as $item) { ?>
<p><?php echo $item; ?></p>
<?php } ?>
</div>
<?php } ?>
<?php if( $has_success ){ ?>
<div class="userprofile_message success">
<?php foreach ($message as $sitem) { ?>
<p><?php echo $sitem; ?></p>
<?php } ?>
</div>
<?php } ?>
</div>
<form action="" method="post" class="usereditprofileform">
<?php wp_nonce_field(\'edit-profile-password-nonce\', \'security\'); ?>
<input type="password" placeholder="old password" value="" name="karneta_currentpass" required>
<input type="password" placeholder="new password" value="" name="karneta_newpass" required>
<input type="password" placeholder="repeat new password" value="" name="karneta_repeatnewpass" required>
<input type="submit" value="change your pass" name="karneta_pass_submit">
</form>
</div>
</div>
最合适的回答,由SO网友:Sh.Dehnavi 整理而成
我自己解决了。Ir是因为user ID
:
错误:
wp\\u set\\u password($newpass,$UserID);
正确:
wp\\u set\\u password($newpass,$user->;ID);
SO网友:Rick Hellewell
嵌套的if/else/elseif通常太复杂,我无法理解。
我会更改您的代码,使用开关/案例来确定正确的输入,如果一切正常,则更改密码。
为了清理$\\u POST(和$\\u GET)输入,我只需将其放在我的函数文件中:
$_GET = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
那我就不必记得在其他地方清理东西了。