WordPress-清除不正确凭据上的登录字段

RPC

我在网上找到了此代码段,可将其插入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));

  jQuery("input#user_pass").val("").blur();

  jQuery("input#user_login").val("").focus();

});

然后在您functions.php的活动孩子/主题中放入以下代码片段,并在登录失败后使用以下挂钩将其插入到登录页面中!

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;
}

add_filter('login_errors', 'my_custom_login_failure');

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章