Support » Plugins » Hacks » Displaying a custom error message

  • Hello,

    I am following along a tutorial in Yannick Lefebvre Plugin Cookbook on saving user-submitted content. The form saves the data in the database correctly, however if a required field is missing, I received the following instead of the abort message I provided (see below):

    Comment Error
    Please enter all fields correctly to post a comment.

    I believe this is the default message for the comment form. The code I have is

    if ( wp_verify_nonce( $_POST['br_user_form'],'add_review_form' ) &&
    !empty( $_POST['book_title'] ) &&
    !empty( $_POST['book_author'] ) &&
    !empty( $_POST['book_review_text'] ) &&
    !empty( $_POST['book_review_book_type'] ) &&
    !empty( $_POST['book_review_rating'] ) ) {
       // Insert data in database and other stuff
    } else {
      // Display message if any required fields are missing
      $abortmessage = 'Some fields were left empty. Please ';
      $abortmessage .= 'go back and complete the form.';

    To confirm that the program reaches the error block, I placed a JavaScript alert just before the $abortmessage variable and it shows up. Therefore, I am reaching that block of code if a field is empty.

    If anyone can shed some light why the custom error message does not show, I will be much appreciated.

    Thanks in advance.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz


    This is more an educated guess than an answer. I’m assuming you are leveraging the post comment system to collect custom user content. The default core required comment field error is actually:

    ERROR: please fill the required fields (name, email).

    I’m guessing the message you are seeing is from your theme’s default. It’s possible for a theme to install its own error handler system. How that may work is anyone’s guess. It could be the theme’s required field check registered an error before yours, so when it came time to display messages, your’s was ignored in favor of the theme’s.

    You probably need to suppress the theme’s required field check as it is likely invalid, and handle it yourself. You need to find the code where the theme does this in order to know how to proceed. It could be a pluggable function where you simply declare your version from a child theme, or there could be a filter or action to facilitate overriding. Or no provision was made at all, forcing you to hack the theme’s code directly.

    Thanks for your reply, BCworkz. Perhaps that is a possibility. I am using the Twenty Eleven theme – a “default” theme – as I work my way through the book. I have not altered any code that relates to the theme itself.

    If I do need to suppress the theme’s required field check, I need to keep searching for answers to learn how to do that.


    Moderator bcworkz


    I’m using twentyeleven as well. The error message I get is the default I mentioned, the theme does not do any additional error trapping. It could also be a plugin interfering.

    How is your code hooked into the comment process? It may just be a matter of hooking before the offending code gets a chance to execute, though it’s more reliable to actually remove their hook than rely on priority.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Displaying a custom error message’ is closed to new replies.