1

I have a block of code that I know contains multiple errors which come from the wpmu_validate_blog_signup function. The error object is part of the $result array.

$result = wpmu_validate_blog_signup($domain_name, $domain_title);

    if (is_wp_error($result['errors'])) {
        $all_errors = $result['errors']->get_error_messages();
        foreach ($all_errors as $key => $value) {
            wc_add_notice($value, 'error');
        }
    }

Is get_error_messages not supposed to return an array of all messages? Shouldn't $value be the error string? Calling $result['errors']->get_error_message() correctly gets the first error string, but I can't seem to figure out how to get them all. Calling $result['errors']->get_all_messages() returns (Array)

Update:

Discovering that this was a multidimensional array, I came up with this code which works:

if (is_wp_error($result['errors'])) {
        $all_errors = $result['errors']->errors;
        //var_dump($all_errors);
        foreach ($all_errors as $key => $value) {
            if ($key == 'blogname'){
                foreach($all_errors[$key] as $key2 => $value){
                    wc_add_notice($value, 'error');
                }
            }
        }
    }

But this feels clunky. What if I don't know the array keys? Feels like there should be a better way to handle this, so please advise if you can see how to make it better.

  • if that code correctly returns 1 error, $all_errors probably only has 1 error. Enable debug_log in wp and debug $all_errors, like this: error_log(' all errors array: '.print_r($all_errors,true).'</pre>');. – Bjorn Jun 10 '18 at 12:03

Your Answer

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

Browse other questions tagged or ask your own question.