如何通过过滤器仅修改函数的一部分

时间:2014-09-17 作者:user300979

我正在尝试编辑验证用户注册的函数的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 );
}
我意识到这是一个插件函数,但我的问题是关于过滤器的,所以我在这里问。

1 个回复
最合适的回答,由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 对象,忽略要更改的错误消息。

健康警告我们正在比较人类可读的字符串,这很可能会在没有宣布的情况下发生变化。这毕竟是一种黑客行为

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\' );
请注意,这完全没有经过测试

结束

相关推荐

get_posts() and filters

我为添加了自定义筛选函数the_posts 在插件中筛选。add_filter(\'the_posts\', \'posts_filter\'); function posts_filter() { … } 这在主循环中运行得很好,这意味着帖子会按照我在posts_filter 作用但我正在打电话get_posts() 在ajax请求中获取一些帖子。在那里,过滤器不起作用。query_posts() 或自定义wp_query 不要工作太多。所以问题是:我如何才能在主循环旁获得按the_p