我正在尝试编辑验证用户注册的函数的a部分,我需要更改错误响应的语言,因为我将sanitize\\u user()修改为不允许“@”和“.”。因此,我需要在..下编辑错误响应。。
//用户名必须通过WP的有效性检查。
并将错误文本响应更改为我选择的内容。我如何连接和修改部分?
function bp_core_validate_user_signup( $user_name, $user_email ) {
// Make sure illegal names include BuddyPress slugs and values
bp_core_flush_illegal_names();
// WordPress Multisite has its own validation. Use it, so that we
// properly mirror restrictions on username, etc.
if ( function_exists( \'wpmu_validate_user_signup\' ) ) {
$result = wpmu_validate_user_signup( $user_name, $user_email );
// When not running Multisite, we perform our own validation. What
// follows reproduces much of the logic of wpmu_validate_user_signup(),
// minus the multisite-specific restrictions on user_login
} else {
$errors = new WP_Error();
// Apply any user_login filters added by BP or other plugins before validating
$user_name = apply_filters( \'pre_user_login\', $user_name );
// User name can\'t be empty
if ( empty( $user_name ) ) {
$errors->add( \'user_name\', __( \'Please enter a username\', \'buddypress\' ) );
}
// user name can\'t be on the blacklist
$illegal_names = get_site_option( \'illegal_names\' );
if ( in_array( $user_name, (array) $illegal_names ) ) {
$errors->add( \'user_name\', __( \'That username is not allowed\', \'buddypress\' ) );
}
// User name must pass WP\'s validity check
if ( ! validate_username( $user_name ) ) {
$errors->add( \'user_name\', __( \'Usernames can contain only letters, numbers, ., -, and @\', \'buddypress\' ) );
}
// Minimum of 4 characters
if ( strlen( $user_name ) < 4 ) {
$errors->add( \'user_name\', __( \'Username must be at least 4 characters\', \'buddypress\' ) );
}
// No underscores. @todo Why not?
if ( false !== strpos( \' \' . $user_name, \'_\' ) ) {
$errors->add( \'user_name\', __( \'Sorry, usernames may not contain the character "_"!\', \'buddypress\' ) );
}
// No usernames that are all numeric. @todo Why?
$match = array();
preg_match( \'/[0-9]*/\', $user_name, $match );
if ( $match[0] == $user_name ) {
$errors->add( \'user_name\', __( \'Sorry, usernames must have letters too!\', \'buddypress\' ) );
}
// Check into signups
$signups = BP_Signup::get( array(
\'user_login\' => $user_name,
) );
$signup = isset( $signups[\'signups\'] ) && ! empty( $signups[\'signups\'][0] ) ? $signups[\'signups\'][0] : false;
// Check if the username has been used already.
if ( username_exists( $user_name ) || ! empty( $signup ) ) {
$errors->add( \'user_name\', __( \'Sorry, that username already exists!\', \'buddypress\' ) );
}
// Validate the email address and process the validation results into
// error messages
$validate_email = bp_core_validate_email_address( $user_email );
bp_core_add_validation_error_messages( $errors, $validate_email );
// Assemble the return array
$result = array(
\'user_name\' => $user_name,
\'user_email\' => $user_email,
\'errors\' => $errors,
);
// Apply WPMU legacy filter
$result = apply_filters( \'wpmu_validate_user_signup\', $result );
}
return apply_filters( \'bp_core_validate_user_signup\', $result );
}
我意识到这是一个插件函数,但我的问题是关于过滤器的,所以我在这里问。
最合适的回答,由SO网友:Stephen Harris 整理而成
问题中的过滤器过滤数组,填充方式如下:
$result = array(
\'user_name\' => $user_name,
\'user_email\' => $user_email,
\'errors\' => $errors,
);
错误消息包含在
WP_Error
与键一起存储的对象
errors
.
这个WP_Error
对象(请参见codex) 在由错误代码索引的专用二维数组中不存储一个或多个错误。每个错误代码可以分配多条错误消息。
不幸的是,您只能添加错误,无法删除它们、它们的消息,甚至无法更改它们(请参阅trac ticket - 如果你幸运的话,我可以提交一个补丁;))。
唯一的解决方案是创建一个新的WP_Error
对象,忽略要更改的错误消息。
健康警告我们正在比较人类可读的字符串,这很可能会在没有宣布的情况下发生变化。这毕竟是一种黑客行为类似地,错误代码可能会更改,恕不另行通知:/观察了BP的代码后,即使上面提到的跟踪记录单有补丁,如果不使用人类可读的字符串,您仍然不能只针对所需的错误消息-所有错误都使用相同的代码考虑到这一点,
function wpse161688_modifity_errors( $result ){
$original_error = $result[\'errors\'];
$new_error = new WP_Error();
foreach( $original_error->get_error_codes() as $code ){
foreach( $original_error->get_error_messages( $code ) as $message ){
if( $code != \'user_name\' || $message != __( \'Usernames can contain only letters, numbers, ., -, and @\', \'buddypress\' ) ){
$new_error->add( $code, $message );
}else{
//This is the (code,message) pair we want to target...
$message = \'... change message here ... \';
$new_error->add( $code, $message );
}
}
}
$result[\'errors\'] = $new_error;
return $result;
}
add_filter( \'wpmu_validate_user_signup\', \'wpse161688_modifity_errors\' );
请注意,这完全没有经过测试