Wordpress注册错误与自定义代码


wordpress registration errors with custom code

有人知道为什么我下面的代码会导致一个致命的错误与以下核心wordpress代码在users.php行2610。下面的代码位于functions.php中,当处理表单时,服务器弹出一个致命错误,提示wp-includes/users.php第2610行。

// check register form for errors
add_filter( 'registration_errors', 'myplugin_registration_errors', 10, 3 );
function myplugin_registration_errors( $errors, $sanitized_user_login, $user_email ) {
    global $errors;
    if (! is_wp_error($errors)) $errors = new WP_Error();
    if ( empty( $sanitized_user_login )) {
        $errors->add( 'user_login_error', __('<strong>ERROR</strong>: The username field is empty' ) );
            return $errors;
    }
    if ( empty( $user_email )) {
        $errors->add( 'user_login_error', __('<strong>ERROR</strong>: Email field is empty' ) );
            return $errors;
    }
    if (username_exists( $sanitized_user_login )) {
        $errors->add( 'user_login_error', __( '<strong>ERROR</strong>: The username you entered is already being used', 'mydomain' ) );
            return $errors;
    }
    if (email_exists($user_email)) {
        $errors->add( 'user_login_error', __( '<strong>ERROR</strong>: The email address you entered is already being used', 'mydomain' ) );
        //'<strong>ERROR</strong>: A user with that email already exists. <a href="' . wp_lostpassword_url() . '">Recover your password</a>', 'mydomain'
            return $errors;
    }
    include("devteamfiles/inc/bannedwordlist.php");
    if (!checkbannedwordlist($sanitized_user_login )) {
        $errors->add( 'user_login_error', __( '<strong>ERROR</strong>: That username can not be used', 'mydomain' ) );
            return $errors;
    }
    if ( ! isset( $_POST['chkagreetos'] )  ) {
        $errors->add( 'tos_aggree_error', __( '<strong>ERROR</strong>: You must agree to site terms of service and disclaimers.', 'mydomain' ) );
            return $errors;
    }
}

Users.php 2610行

$errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email );
    if ( $errors->get_error_code() )
        return $errors;
    $user_pass = wp_generate_password( 12, false );
    $user_id = wp_create_user( $sanitized_user_login, $user_pass, $user_email );
    if ( ! $user_id || is_wp_error( $user_id ) ) {
        $errors->add( 'registerfail', sprintf( __( '<strong>ERROR</strong>: Couldn&#8217;t register you&hellip; please contact the <a href="mailto:%s">webmaster</a> !' ), get_option( 'admin_email' ) ) );
        return $errors;
    }

根据提供的答案编辑代码,但仍然弹出致命错误Fatal error: Call to a member function get_error_code() on a non-object in /wp-includes/user.php on line 2610

// check register form for errors
add_filter( 'registration_errors', 'myplugin_registration_errors', 10, 3 );
function myplugin_registration_errors( $errors, $sanitized_user_login, $user_email ) {
    if (! is_wp_error($errors)) $errors = new WP_Error();
    if (username_exists( $sanitized_user_login )) {
        $errors->add( 'user_login_error', __( '<strong>ERROR</strong>: STATEMENT 3', 'mydomain' ) );
            return $errors;
    }
    if (email_exists($user_email)) {
        $errors->add( 'user_login_error', __( '<strong>ERROR</strong>:  STATEMENT 4', 'mydomain' ) );
        //'<strong>ERROR</strong>: A user with that email already exists. <a href="' . wp_lostpassword_url() . '">Recover your password</a>', 'mydomain'
            return $errors;
    }
    include("devteamfiles/inc/bannedwordlist.php");
    if (!checkbannedwordlist($sanitized_user_login )) {
        $errors->add( 'user_login_error', __( '<strong>ERROR</strong>: STATEMENT 5', 'mydomain' ) );
            return $errors;
    }
    if ( ! isset( $_POST['chkagreetos'] )  ) {
        $errors->add( 'tos_aggree_error', __( '<strong>ERROR</strong>: STATEMENT 6', 'mydomain' ) );
            return $errors;
    }
}

您正在传递正确的变量$errors,但您正在用全局变量$errors覆盖它,该变量可能不是对象。

 function myplugin_registration_errors( $errors, $sanitized_user_login, $user_email ) {
//global $errors;
//if (! is_wp_error($errors)) $errors = new WP_Error();
if ( empty( $sanitized_user_login )) {
    $errors->add( 'user_login_error', __('<strong>ERROR</strong>: The username field is empty' ) );
        return $errors;
}
............
//end with returning $errors!!
return $errors;
}