1

I have a custom front end page where users can post from. All works fine but the i want if more than one field is empty then the errors for all empty fields should show all together. Right now the code only show error for the first empty field it detects. The code is :

    $errors = new WP_Error();

    if (isEmptyString($title_stripped))
    $errors->add('empty_title', __('Please enter a title'));

    if (isEmptyString($description_stripped))
    $errors->add('empty_desc', __('Enter a breif description'));

    if (isEmptyString($content_stripped))
    $errors->add('empty_content', __('Please enter your article here'));

    return $errors;

Edit : Solved

Sorry to bother . It was one of my other functions which was limiting the error to echo only once. Here is the previous code :

function display_message( $message = false ) {
    if( is_wp_error( $message ) ) {
        echo '<div class="error"><p>' . $message->get_error_message() . '</p></div>';  
    }

It worked fine when i changed it too :

function display_message( $message = false ) {
    if( is_wp_error( $message ) ) {
        echo '<div class="error"><p>';
        foreach ( $message->get_error_messages($code) as $error ) {
        echo $error . "<br />\n";  
        }
        echo '</p></div>';  
    }
1
  • Great that you solved your own problem. Could you please add it as an "answer" instead of editing this question? This way the question won't stay in the "Unanswered" list.
    – Jan Fabry
    May 27, 2011 at 13:38

1 Answer 1

0

Sorry to bother . It was one of my other functions which was limiting the error to echo only once. Here is the previous code :

function display_message( $message = false ) {
    if( is_wp_error( $message ) ) {
        echo '<div class="error"><p>' . $message->get_error_message() . '</p></div>';   
    }

It worked fine when i changed it too :

function display_message( $message = false ) {
    if( is_wp_error( $message ) ) {
        echo '<div class="error"><p>';
        foreach ( $message->get_error_messages($code) as $error ) {
        echo $error . "<br />\n";   
        }
        echo '</p></div>';  
    }

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.