自定义登录错误消息

时间:2021-10-13 作者:ceetee

在哪里以及如何自定义登录错误消息?

"E;错误:用户名(<&燃气轮机;未在此网站上注册。如果您不确定您的用户名,请尝试您的电子邮件地址"E;

和;错误:您为用户名输入的密码<&燃气轮机;不正确。丢失密码"E;

我试过使用这个代码,但什么都没有。任何帮助都将不胜感激!

    global $errors;
    $err_codes = $errors->get_error_codes();

    // Invalid username.
    if ( in_array( \'invalid_username\', $err_codes ) ) {
        $error = \'<strong>ERROR</strong>: This email address is not registered. Please check or contact XXX for more information.\';
    }

    // Incorrect password.
    if ( in_array( \'incorrect_password\', $err_codes ) ) {
        $error = \'<strong>ERROR</strong>: The password you provided is incorrect. Please try again or contact XXX for more information. \';
    }

    return $error;
}

add_filter( \'login_errors\', \'my_custom_error_messages\');```

1 个回复
SO网友:Tommy Pradana

我看不到你的完整代码。。。但是,您的代码应该如下所示:

function my_custom_error_messages( $error ) {
    global $errors;
    $err_codes = $errors->get_error_codes();
 
    // Invalid username.
    // Default: \'<strong>ERROR</strong>: Invalid username. <a href="%s">Lost your password</a>?\'
    if ( in_array( \'invalid_username\', $err_codes ) ) {
        $error = \'<strong>ERROR</strong>: Wrong username.\';
    }
 
    // Incorrect password.
    // Default: \'<strong>ERROR</strong>: The password you entered for the username <strong>%1$s</strong> is incorrect. <a href="%2$s">Lost your password</a>?\'
    if ( in_array( \'incorrect_password\', $err_codes ) ) {
        $error = \'<strong>ERROR</strong>: Wrong password.\';
    }
 
    return $error;
};
add_filter( \'login_errors\', \'my_custom_error_messages\' );
以上代码已经过测试和使用。

相关推荐

如何创建一个函数来修改wp-login.php文件中的消息?

当用户使用WordPress中的密码重置功能时,会要求他们输入电子邮件并单击确认。这样做之后,他们得到的信息是:检查电子邮件中的确认链接,然后访问登录页面。我希望修改此消息,并且我已经找到了在wp登录中写入消息的代码行。php文件:if ( \'confirm\' === $_GET[\'checkemail\'] ) { $errors->add( \'confirm\', sprintf( /* translators: %s: L