您可以通过\'registration_errors\' 过滤器挂钩。钩子过滤保存所有当前错误的WP\\u Error对象。使用的代码如下所示:
add_filter( \'registration_errors\', function( $errors ){
if( $errors->get_error_messages( \'invalid_username\' ) ) {
$errors->remove(\'invalid_username\');
$errors->add(\'invalid_username\', \'<strong>Error</strong>: My custom error message\');
}
return $errors;
});
Please note 我们通过其代码检查目标错误消息的存在;\\u用户名无效;,但是,如果在“非法用户登录”数组中发现此代码,则该代码可能包含不同的用户名消息,该数组可能包含不允许的用户名列表,因此如果您需要不同的不允许的用户名错误消息,可以使用第二个参数$sanitized\\u user\\u登录”;检查它是否在不允许列表中,如果是,则更改错误消息。您的代码可能如下所示:
add_filter( \'registration_errors\', function( $errors, $sanitized_user_login ){
if( $errors->get_error_messages( \'invalid_username\' ) ) {
// Get the list of disallowed usernames
$illegal_user_logins = array_map(\'strtolower\', (array) apply_filters( \'illegal_user_logins\', array() ));
// Set our default message
$message = \'<strong>Error</strong>: My custom error message\';
// Change the message if the current username is one of the disallowed usernames
if( in_array( strtolower( $sanitized_user_login ), $illegal_user_logins, true) ) {
$message = \'<strong>Error</strong>: My custom error message 2\';
}
$errors->remove(\'invalid_username\');
$errors->add(\'invalid_username\', $message);
}
return $errors;
}, 10, 2);