我正在尝试自定义我的 Wordpress 主题中的“丢失密码”区域,称为 Enfold。一切都好,所以我的代码正确地向用户电子邮件发送了一个指向“新密码介绍”区域的链接。链接是:/new-password/?action=rp&key=TDeJEj7vVmmmJqbd&login=my_username
但是当用户点击这个链接并且代码将他重定向到“[新密码介绍]/[重复密码]”区域时,在用户写入新密码和密码确认后执行AJAX代码的按钮没有响应。
我已将 wp_register_script 和 wp_enqueue_script 添加到 functions.php 但我认为这可能是我的错误。我是这样添加的:
if(!function_exists('avia_register_frontend_scripts'))
{
if(!is_admin()){
add_action('wp_enqueue_scripts', 'avia_register_frontend_scripts');
}
function avia_register_frontend_scripts()
{
$template_url = get_template_directory_uri();
$child_theme_url = get_stylesheet_directory_uri();
//register js
wp_register_script( 'avia-compat', $template_url.'/js/avia-compat.js', array('jquery'), 1, false ); //needs to be loaded at the top to prevent bugs
wp_register_script( 'avia-default', $template_url.'/js/avia.js', array('jquery'), 1, true );
wp_register_script( 'avia-shortcodes', $template_url.'/js/shortcodes.js', array('jquery'), 1, true );
wp_register_script( 'avia-prettyPhoto', $template_url.'/js/prettyPhoto/js/jquery.prettyPhoto.js', 'jquery', "3.1.5", true);
// wp_register_script( 'wp-mediaelement', $template_url.'/js/mediaelement/mediaelement-and-player.min.js', 'jquery', "1", true);
wp_register_script( 'discount', $template_url.'/js/discount.js', 'javascript', "1", true);
wp_register_script( 'reset_user_pass', $template_url.'/js/reset-user-pass.js','jquery', "1", true );
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'avia-compat' );
wp_enqueue_script( 'avia-default' );
wp_enqueue_script( 'avia-shortcodes' );
wp_enqueue_script( 'avia-prettyPhoto' );
wp_enqueue_script( 'wp-mediaelement' );
wp_enqueue_script( 'reset_user_pass' );
这里是“[新密码介绍]/[重复密码]”表单代码:
<?php
/*Load Scripts for password reset page*/
wp_enqueue_script( 'zxcvbn-async' );
wp_enqueue_script( 'user-profile' );
wp_enqueue_script( 'password-strength-meter' );
wp_enqueue_script( 'user-suggest' );
?>
<form method="post" action="<?php echo get_bloginfo('url') ?>/wp-login.php" id="resetpassform" name="resetpassform">
<input type="hidden" name="login" value="<?php echo $_GET['login'] ?>" autocomplete="off">
<input type="hidden" name="key" value="<?php echo strip_tags($_GET['key']); ?>" />
<p style="margin-bottom:20px" class="description indicator-hint">Your password needs to be at least seven characters. Mixing upper and lower case, numbers and symbols like ! " ? $ % ^ & ) will make it stronger.</p>
<p class="login-username">
<input type="password" tabindex="10" size="20" value="" placeholder="New Password"class="input" id="pass1" name="pass1">
</p>
<p class="login-password">
<input type="password" tabindex="20" size="20" value="" placeholder="Confirm Password" class="input" id="pass2" name="pass2">
</p>
<div class="pass-meter"><div id="pass-strength-result" ><?php _e('Strength indicator'); ?></div></div>
<p class="forgotpass-submit">
<a id="forgot-cancel" href="<?php echo home_url('/signin'); ?>">Cancel</a>
<a id="submitforgotpasswordform" href="javascript:void(0)" style="background-position: 0px 4px;"><input type="submit" tabindex="100" value="Get New Password" id="forgot-submit" name="wp-submit"></a>
</p>
<div class="login-error"><div></div></div>
</form>
这是在我的functions.php中重置验证传递的php代码:
function reset_user_pass(){
parse_str( $_POST['form_values'], $params );
$user = check_password_reset_key($params['key'], $params['login']);
$status='';
// Check if key is valid
if ( is_wp_error($user) ) {
if ( $user->get_error_code() === 'expired_key' ){
$status = 'expiredkey' ;
}
else{
$status = 'invalidkey' ;
}
echo $status;
die;
}
// check if keys match
if ( isset($params['pass1']) && $params['pass1'] != $params['pass2'] ){
$status = 'mismatch';
}else{
// Update the user pass
reset_password($user, $params['pass1']);
$status ='success';
}
echo $status;
die;
}
add_action('wp_ajax_nopriv_reset_user_pass', 'reset_user_pass');
最后是没有响应提交按钮的 AJAX 代码:
(function($){
$(document).ready(function() {
// Submit the password reset form via ajax
$( '#resetpassform' ).submit(function(e){
e.preventDefault();
$('.login-error').slideUp();
//check if password fields are empty
if( $('#pass1').val()=='' || $('#pass1').val()=='' ){
return false;
}
var formData= $(this).serialize();
$.ajax({
url: ajaxurl,
type: 'POST',
data: {form_values: formData, action:'reset_user_pass' },
})
.done(function(status) {
switch(status){
case 'expiredkey' :
case 'invalidkey' :
$('.login-error').html('<div>Sorry, the link does not appear to be valid or is expired.</div>').slideDown();
break;
case 'mismatch' :
$('.login-error').html('<div>The passwords do not match.</div>').slideDown();
break;
case 'success' :
$('.login-error').html('<div>Your password has been reset.</div>').slideDown();
break;
default:
console.log(status);
$('.login-error').html('<div>Something went wrong.Please try again </div>').slideDown();
break;
}
})
.fail(function() {
console.log("error");
})
.always(function() {
console.log("complete");
});
})
})
})(jQuery)
非常感谢您的帮助和时间。