我在网上找到了这个代码片段,将其插入到我的
function.php
文件中以自定义错误消息,使其更安全:
// custom the login error message
function customize_login_errors(){
return 'The login credentials are incorrect.';
}
add_filter( 'login_errors', 'customize_login_errors' );
虽然,我发现了一个漏洞。
如果用户名不正确,用户名输入字段会与密码一起被清除。完美,正是我想要的。但是,如果我正确输入用户名,则在输入无效密码时该字段的输入仍然保留。尽管这比默认错误消息更安全,但它仍然会让不受欢迎的访客知道他们是否猜到了有效的用户名。
输入无效凭据后,如何更进一步并
?clear both the username/email and password fields
当登录失败时,我会将以下 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;
}
Sé que esta pregunta tiene dos años y que estoy hablando español (disculpen)。 Pero para todo el que estébuscandoestasolución, es mucho más fácil hacer esto:
function failed_login()
{
$_POST['log'] = '';
return 'My custom error message';
}
add_filter('login_errors', 'failed_login');
Asignando $_POST['log'] = '' 消除用户名错误和验证错误。 Antes puedes manejar errores si quieres.
A mí me ha funcionado esto.