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:
- Use an ajax based plug-in, like WP-Ajaxify-Comments, that uses Javascript to display an error message on the page with the form instead of directing the user to the WordPress default error display.
- 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.
Each of these has advantages and disadvantages. An ajax solution gives a good end user experience because it shows the errors without reloading the page, but it requires JavaScript which can be disabled by the end user. A custom error message template offers a more customizable way to display the error message but WordPress is still redirecting the user away from the comment form and the user must click a link to get back to the form. Alternative commenting systems avoid the WordPress quirks but may not support the features you need for your site, or maybe you just don’t want 3rd parties involved in your web site comments.
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.
However, using WordPress API functions and PHP sessions we can support a purely PHP based solution that does not require JavaScript or redirecting the end user to a error message page. Here is a simplified overview of the process:
- 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_post
filter 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_Error
object. Some WordPress functions can accept a WP_Error object as an argument and pass it along to other functions. - Use a WordPress
wp_die_handler
filter 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$_POST
data from the form in a$_SESSION
variable, 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_redirect
to retrieve the error data and$_POST
data from the$_SESSION
variable. 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_top
to 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.