1

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'.

<?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.

  • Please explain what you are trying to do-- the end result. That is, you are trying to get the messages into separate variable but why? I can't think of too many reasons that you would need to do that. And see the following about how filters work: wordpress.stackexchange.com/a/103644/21376 – s_ha_dum Oct 27 '13 at 15:18
  • @s_ha_dum I want to assign the error messages to separate variables because that way I can display each error message next to the related form field. As I said above, I'm able to display all errors with above foreach loop but I'm unable to figure out how to assign them in different variables. – jay Oct 27 '13 at 15:22
  • You don't need to assign them to variables to display them next to your form fields. That is an extra unnecessary step. – s_ha_dum Oct 27 '13 at 15:24
  • @s_ha_dum Can you please tell how I can do then? I edited the question so that its a bit more clear. Will appreciate any help. – jay Oct 27 '13 at 15:30
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.