WordPress 如何清除错误凭据上的登录字段
WordPress how to clear the login fields on incorrect credentials
我在网上找到了这个代码片段,将其插入到我的 function.php
文件中以自定义错误消息,使其更安全:
// custom the login error message
function customize_login_errors(){
return 'The login credentials are incorrect.';
}
add_filter( 'login_errors', 'customize_login_errors' );
虽然,我发现了一个漏洞
如果用户名不正确,用户名输入字段会连同密码一起被清除。完美,正是我想要的。但是,如果我正确输入用户名,输入无效密码后该字段的输入仍然存在。尽管这比默认的错误消息更安全,但它仍然会让不速之客知道他们是否猜到了有效的用户名。
How do I take it a step further and clear both the username/email and password fields
upon entering invalid credentials?
我会在登录失败时将以下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();
});
然后将以下代码片段放入您的活动 child/theme 的 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;
}
我在网上找到了这个代码片段,将其插入到我的 function.php
文件中以自定义错误消息,使其更安全:
// custom the login error message
function customize_login_errors(){
return 'The login credentials are incorrect.';
}
add_filter( 'login_errors', 'customize_login_errors' );
虽然,我发现了一个漏洞
如果用户名不正确,用户名输入字段会连同密码一起被清除。完美,正是我想要的。但是,如果我正确输入用户名,输入无效密码后该字段的输入仍然存在。尽管这比默认的错误消息更安全,但它仍然会让不速之客知道他们是否猜到了有效的用户名。
How do I take it a step further and
clear both the username/email and password fields
upon entering invalid credentials?
我会在登录失败时将以下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();
});
然后将以下代码片段放入您的活动 child/theme 的 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;
}