ошибки регистрации WordPress с пользовательским кодом

Кто-нибудь знает, почему мой следующий код вызовет фатальную ошибку со следующим основным кодом 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;
}
}

0

Решение

Вы передаете правильную переменную $ 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;
}
1

Другие решения

Других решений пока нет …