How do I display WP_Error()
errors on another page?
In functions.php
, I have a form processor with the following code:
global $current_user, $errors; get_currentuserinfo();
$errors = new WP_Error();
if( isset( $_POST['user_email']) ) {
if( !is_email( $_POST['user_email'] ) ) {
$errors->add('invalid_email', __('<strong>ERROR</strong>: The email address isn’t correct.'));
} elseif( email_exists( $_POST['user_email'] ) ) {
$errors->add('email_exists', __('<strong>ERROR</strong>: This email is already registered, please choose another one.'));
} elseif( $_POST['user_email'] <> $current_user->{user_email} ) {
wp_update_user( array( 'ID' => $uid, 'user_email' => esc_attr( $_POST['user_email'] ) ) );
}
}
if($errors->get_error_code()) { return $errors; }
In account.php
, which displays the form, I have the following code:
<?php
$return = update_account();
if(is_wp_error($return)) { echo $return->get_error_message(); }
?>
Neither the invalid_email
nor the email_exist
errors are reported during testing. However, errors are prevented. I just don’t understand why they’re not being displayed. What am I doing wrong?
You need to put global $errors on your account page.