当登录失败时,我会将以下 javascript 片段注入登录页面!
把这个 javascript 放到一个名为的文件中,比如说,custom_error_login.js
jQuery(document).ready(async function ($) {
await new Promise(r => setTimeout(r, 200));
$("input#user_pass").val("").blur();
$("input#user_login").val("").focus();
});
然后将以下代码段放入您functions.php的活动子/主题中,并在登录失败后使用以下钩子将其注入登录页面!!!
add_filter('login_errors', 'my_custom_login_failure');
function my_custom_login_failure()
{
global $errors;
$error_codes = $errors->get_error_codes();
// Invalid username.
if (in_array('invalid_username', $error_codes)) {
$error = '<strong>Invalid credentials!!!</strong>';
}
// Incorrect password.
if (in_array('incorrect_password', $error_codes)) {
$error = '<strong>Invalid credentials!!!</strong>';
}
remove_action('login_footer', 'wp_shake_js', 12); // removing the shaking effect of the form, snippet could work without this line too!!!
wp_enqueue_script('jquery');
wp_enqueue_script('my_custom_login_script', get_theme_file_uri('path/to/js_folder/custom_error_login.js'), 'JQuery', "1.0", TRUE);
return $error;
}