Managing Comment Form Errors
Posted on August 5, 2013
A common complaint about the WordPress comment form is the arguably “ungraceful” way that it handles error messages for incorrectly filled out comment fields. If your site does not have a comment error plug-in or your theme does not have the required coding to support a custom error template, then a visitor on your site who incorrectly fills out the comment form will see the following error message display.
As you can see, the error message does not use the layout of the theme of the site and has no link back to the post, which leaves much to be desired as far as an end user experience.
Common solutions to this problem are:
- Create a wp_die_handler function to load a custom error message template that displays the error message within layout of the theme of the site. The user is still redirected to an error page but at least the error page looks like your web site and can include a link back to the post that the user was trying to comment on.
- Use an alternative commenting system like disqus and avoid using WordPress to manage comments all together.
The most common approach for validating and displaying errors in a web form is to use a server side process to validate the data submitted through the form and if there are errors, reprint the form fields with the user submitted data and error messages. Unfortunately this is awkward to do with WordPress because WordPress submits the comment form data to a different url than the one with the page displaying the comment form, and we cannot easily reprint the form data and error messages.
- User attempts to post a comment. The comment form submits the form data via HTTP POST to the wp-comments-post.php script, which will attempt to validate the name, email and comment fields and save the comment.
- Use WordPress Comment Form
pre_comment_on_postfilter to call our custom functions that validate the required comment form fields, including any custom meta fields, using our criteria. WordPress executes this filter after the user submits the comment form, and before it executes code in the wp-comments-post.php script to validate comment data and save the data. This is the stage where we need to intercept the data, and identify any errors and decide how to handle them.
- If there are errors, store the errors in a
WP_Errorobject. Some WordPress functions can accept a WP_Error object as an argument and pass it along to other functions.
- Use a WordPress
wp_die_handlerfilter to call a custom function to handle the errors. We can pass the error object to this function. It will store the error data and the
$_POSTdata from the form in a
$_SESSIONvariable, which is an idiomatic way to share information across different scripts or pages of a web site. It then redirects the user back to the comment form on the page that the user was attempting to post a comment on. The handler also terminates the wp-comments-post.php script to prevent WordPress from saving the erroneous comment data.
- Once we are back on the page with the comment form, use a WordPress action like
template_redirectto retrieve the error data and
$_POSTdata from the
$_SESSIONvariable. We can use use the error data to print error messages on the page and add the user data back into the comment form fields so the user does not have to retype all of their information.
- Print the error in whatever manner we feel is suitable. For example, we can use a comment form action like
comment_form_topto call a function that prints the errors as a list into the form at a location between the title and first field.
- Now the user can correct the error and try re-posting the comment.
Of course there are lots of little issues that come up throughout the process and things are a bit more complex, like what to do if sessions are disabled and how to make it easy to set up our own validation functions. So I ended up making the WP Inline Comment Errors plug-in to simplify the process for other people who want a PHP based solution. Learn more about how you can use the plug-in to improve your web site’s comment form.