wordpress注册错误与自定义代码

时间:2015-09-07 22:11:29

标签: php wordpress custom-errors

任何人都知道为什么我的以下代码会在users.php第2610行中使用以下核心wordpress代码导致致命错误。下面的代码在functions.php中,当处理表单时,服务器弹出wp的fatel错误-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;
    }
}

1 个答案:

答案 0 :(得分:1)

您正在传递正确的变量$ 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;
}