How to customize wordpress “comment error” page

If you enter incorrect information into the comment forms (missing name, email etc), wordpress returns an empty page with the relevant error message. I googled, but couldn’t find a way to customize this response.

Does anyone know how to do it?

Related posts

Leave a Reply

4 comments

  1. For this particular problem, I ended up modifying wp-comments-post.php directly – the theme engine doesn’t allow you to customize it.

    The short description of what I did is to bypass the usage of “wp_die()” and instead have the script execute “my_wp_die()” in scenarios when the blog comment post does not validate.

    Here’s a sequential example:

    1) User lands on article page (invoking single.php in your theme)
    2) User enters some info into the comments fields (may or may not validate)
    3) Form posts to “wp-comments-post.php” with the article’s URL as a parameter so it can pass back validation results to a page which has the proper visuals (sidebar: I wasn’t able to hook in an outside template library I wrote which gets integrated into the theme instead of duplicating my site’s template into the theme) and the user form fields.
    4) wp-comments-post.php will call my_wp_die() when not validating (includes a dupe check from /comment.php:function wp_allow_comment()), or my_wp_success() if the comment is valid and gets posted.
    5) wp-comments-post.php will forward the user back to the article URL (originating permalink).
    6) single.php in the theme will display error/success message and new comment.

  2. I was unable to find anything on customizing the error page simplistically however a quick glance at wp-comments-post.php tells me that when it runs into an error it executes the function wp_die which generates the error page. It seems to take some arguments which control the output of the error page but couldn’t find any details on what those option might be. With a little tweaking of wp_die you should be able to get it to display your own custom error page. Not sure if this will help but it’s a place to start.

  3. On a related note, make sure to verify that your error page appears correctly in Internet Explorer. I only mention this because I recently experienced the problem and unfortunately spent several hours figuring it out.

    In a nutshell, the generic error page that WordPress generates (e.g., after “user comment” validation fails) is sent to the browser with an HTTP status code of “500 Internal Server Error.” Oddly, Internet Explorer (prior to version 7) will ignore error pages sent with “500” if the page amounts to less than 512 bytes, displaying the generic Microsoft “This page cannot be displayed” error message instead.

    The WordPress guys know abou this bug and wrote the wp_die() function such that it ensures the error page is bigger than 512 bytes. Unfortunately they neglected to account for web servers that use gzip compression (i.e., shrink the web page before it’s sent over the network) for performance. Internet Explorer 5/6 will not display generic WordPress (v2.7 and earlier) error pages if the web server is using gzip compression.

    If it’s any help to other folks who come across this, I’ve written up a blog post describing the problem in more detail (including screenshots), including a link to the WordPress bug I opened and instructions for a temporary work-around that can be used until the bug is properly fixed. See http://www.clintharris.net/2009/ie-512-byte-error-pages-and-wordpress/ for more info.

  4. I’ve just been puzzling over this myself, the latest version of WP doesn’t seem to have any further enhancements as far as I can see.

    However, my solution was to do some client side form validation in Javascript. Just attach an event handler to the submit button (eg. with submit.onclick) and then link it to your JS validate function to check the data and provide feedback if it fails (like a red border around offending form field).

    Remember to use preventDefault if the data fails validation so that the form doesn’t get submitted!

    Of course the form will still submit if Javascript is not enabled, and in that case the user will still see the default WP error page. To work around that you could pursue a PHP option as previously discussed, but I think that’s more work.