组合示例(在5.2中测试)
步骤1:
前端表单
<form id="form-password-reset" action="<?php echo wp_lostpassword_url(); ?>" method="post">
<?php wp_nonce_field( \'ajax-resetpassword-nonce\', \'security\' ); ?>
<div class="text-align-center margin-bottom-32">
<h3 class="h3">Reset Your Password</h3>
</div>
<div class="row">
<div class="col-12 col-md-4">
<div class="form-group">
<label for="auth-password-reset-email">Email</label>
<div class="form-input-wrapper">
<input type="email" class="form-input" id="auth-password-reset-email" name="email" placeholder="eg. [email protected]">
</div>
</div>
<button type="submit" class="btn-solid btn-primary btn-block">
Submit</button>
<p class="form__error margin-top-16">
// TODO you can show the message here
</p>
</div>
<div class="col-12 col-md-4"></div>
</div>
</form>
<使用Js处理ajax提交
$(form).on(\'submit\', function (event) {
event.preventDefault(event)
const email = $(`#auth-password-reset-email`).val()
const security = $(`#security`).val()
const action = \'authResetPassword\'
var has_error = false;
if (email.length <= 0) {
// TODO
has_error = true;
}
if (security.length <= 0) {
// TODO
has_error = true;
}
if (has_error == true) {
// TODO
return false
}
$.ajax({
type: \'POST\',
dataType: \'json\',
url: ajax.ajaxurl,
data: { action, email, security },
success: function (data) {
if (data.status) {
// TODO
}
if (!data.status) {
// TODO
}
}
})
return false;
})
服务器端生成重置链接并触发电子邮件
try {
check_ajax_referer(\'ajax-resetpassword-nonce\', \'security\');
$response = [
"status" => false,
"error" => true,
"data" => "",
];
extract($_POST);
$user_email = sanitize_text_field($email);
$user = get_user_by( \'email\', $user_email );
if( $user instanceof WP_User ) {
$user_id = $user->ID;
$user_info = get_userdata($user_id);
$unique = get_password_reset_key( $user_info );
$unique_url = network_site_url("wp-login.php?action=rp&key=$unique&login=" . rawurlencode($user_info->user_login), \'login\');
$subject = "Reset Password Link";
$message = __(\'Someone requested that the password be reset for the following account:\') . "\\r\\n\\r\\n";
$message = "<p>Hi ".ucfirst( $user_info->first_name ).",</p>";
$message .= network_home_url( \'/\' ) . "\\r\\n\\r\\n";
$message .= sprintf(__(\'Username: %s\'), $user_info->user_login) . "\\r\\n\\r\\n";
$message .= __(\'If this was a mistake, just ignore this email and nothing will happen.\') . "\\r\\n\\r\\n";
$message .= __(\'To reset your password, visit the following address:\') . "\\r\\n\\r\\n";
$message .= $unique_url;
wp_mail( $user_email, $subject, $message ); // TODO
$response[\'data\'] = [ "message" => __(\'Password reset link sent.\'), \'reset_link\' => $unique_url ];
wp_send_json($response);
}
$response[\'data\'] = [ "message" => __(\'Email address not exists.\') ];
wp_send_json($response);
} catch (Exception $e) {
wp_send_json([
"status" => false,
"error" => false,
"data" => ["message" => $e->getMessage() ]
]);
}
注意:显示您自己的自定义重置密码表单
add_action( \'login_form_lostpassword\', \'redirect_to_custom_lostpassword\' );
function redirect_to_custom_lostpassword() {
if ( \'GET\' == $_SERVER[\'REQUEST_METHOD\'] ) {
if ( is_user_logged_in() ) {
// TODO
exit;
}
wp_redirect( home_url( \'custom-password-lost\' ) ); // TODO
exit;
}
}