WordPress Hook钩子 login_errors 登录错误提示信息
WordPress 登录失败的时候,会出现错误提示信息,如果想要修改这些信息,可以使用 Wordpress hook钩子 login_errors 来实现。
在主题 functions.php 文件中引用如下代码:
function failed_login() {
return '自定义错误提示信息';
}
add_filter('login_errors', 'failed_login');
同样,你也可以禁止此类提示
add_filter('login_errors', create_function('$a', "return null;"));
再来看一个Wordpress官方文档提供的一个例子,回调函数替换了“密码提示”的登录错误提示:
add_filter( 'login_errors', function( $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>: Invalid 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>: The password you entered is incorrect.';
}
return $error;
} );