Get errors from WP_Error to different variables

I have added multiple errors in the WP_ERROR. I want to get those errors separately in different variables. With the following code I can get all errors with foreach loop, but how can I assign them to separate variables.

The errors I have added are ‘login_error‘ and ‘email_error‘.

Read More
<?php
    $return = my_custom_function();
    if ( is_wp_error($return) ){

         foreach ( $return -> get_error_messages() as $error ) {
            echo $error;
         }      
    }
?>

I have tried to add this in the above loop:

$login_error = apply_filters('login_error', $error); 
$email_error = apply_filters('email_error', $error); 

But it assign same error to both variables.

Edit:

When a form is submitted and any field has error, I’m adding errors this way:

$errors -> add( 'login_error', __( 'Please type your username' ) );
$errors -> add( 'email_error', __( 'Please type your e-mail address.' ) );   

 if ( $errors->get_error_code() ){          
            return $errors;
        }

After that, I want to display the above errors next to each of the form field, that’s whay I want to get the errors separately.

Related posts

1 comment

  1. Shoving the error data into independent variable is a waste of effort. You already have the data you need in the WP_Error object and could get at it with pure PHP object and array syntax if you wanted, but let’s look at the methods the object provides for retrieving data (with notes copied from the Codex):

    $errors = new WP_Error;
    $errors -> add( 'login_error', __( 'Please type your username' ) );
    $errors -> add( 'email_error', __( 'Please type your e-mail address.' ) );   
    
    var_dump($errors->get_error_codes());
    //     Retrieve all error codes. Access public, returns array List of error codes, if available. 
    var_dump($errors->get_error_code());
    //     Retrieve first error code available. Access public, returns string, int or Empty if there is no error codes
    var_dump($errors->get_error_messages('login_error'));
    //     Retrieve all error messages or error messages matching code. Access public, returns an array of error strings on success, or empty array on failure (if using code parameter) 
    var_dump($errors->get_error_message('login_error'));
    //     Get single error message. This will get the first message available for the code. If no code is given then the first code available will be used. Returns an error string. 
    var_dump($errors->get_error_data('login_error'));
    //     Retrieve error data for error code. Returns mixed or null, if no errors. 
    

    If you look at that output you should immediately spot several options:

    array(2) {
      [0]=>
      string(11) "login_error"
      [1]=>
      string(11) "email_error"
    }
    string(11) "login_error"
    array(1) {
      [0]=>
      string(25) "Please type your username"
    }
    string(25) "Please type your username"
    NULL
    

    For example, in your form near the username field…

    // username field
    echo implode(', ',$errors->get_error_messages('login_error')); // empty string if no error; aka prints nothing if no error
    

    I am not sure what your complete implementation looks like. You almost certainly need something more complicated but that is the idea.

Comments are closed.