Creating a custom comment error page in WordPress

I had wanted to make a custom comment error page on my blog for at least a year, and finally got around to doing it the other week. Here is how you can do it yourself! There are some limitations, but they aren’t deal-breakers either.

You might be familiar with the page looking like this when you try to submit a comment without filling in all the required fields:

Default WordPress error page.
Default WordPress error page.

The code for this page is in wp-includes/functions.php but we don’t want to touch core files, so let’s work with our own functions.php file instead. You should already have one in your WordPress theme’s folder – if not, create one. We will be writing in PHP.

The original function needs to be overridden, so we need to write our own function to override it.

function custom_comment_error( $message, $title='', $args=array() ) {
  $errorTemplate = get_theme_root().'/'.get_template().'/commenterror.php';
  $defaults = array( 'response' => 500 );
  $r = wp_parse_args($args, $defaults);

  $have_gettext = function_exists('__');

  if ( function_exists( 'is_wp_error' ) && is_wp_error( $message ) ) {
    if ( empty( $title ) ) {
      $error_data = $message->get_error_data();
      if ( is_array( $error_data ) && isset( $error_data['title'] ) )
        $title = $error_data['title'];
    }
    $errors = $message->get_error_messages();
    switch ( count( $errors ) ) {
    case 0 :
      $message = '';
      break;
    case 1 :
      $message = "<p>{$errors[0]}</p>";
      break;
    default :
      $message = "<ul>\n\t\t<li>" . join( "</li>\n\t\t<li>", $errors ) . "\n\t";
      break;
    }

  } elseif ( is_string( $message ) ) {
    $message = "<p>$message</p>";
  }

  require_once( $errorTemplate );
  die();
}

Most of the above code is copied from the original function itself, except for the variable for the error template, and the call to the template itself (just before die() is called). This ensures that you can still grab the error messages.

Creating your own error page template

I am assuming that you know how to build a HTML page so you know just what to put in your commenterror.php file. You can name the file whatever you like, just make sure you update the variable in the function, and include the error page template in your theme folder as well.

For the error message to display in your desired location on the page, use <?php echo $message; ?>. The message will be in a paragraph by default, unless you modify the above function to style it otherwise.

Return the function

We now need to create a function to return our custom_comment_error function. Although it seems unnecessary, we need it for the function to work. Make sure this function name is different. I have called it get_custom_comment_error. Put this directly after the function in your functions.php file.

function get_custom_comment_error() {
  return 'custom_comment_error';
}

Now, replace the original function with ours

Then we use a filter to replace the original wp_die_handler function with our one.

add_filter( 'wp_die_handler', 'get_custom_comment_error' );

That’s all!

It’s pretty simple. Here is mine – of course you can get as creative as you want. I am not using the custom error messaging.

Custom comment error page for Hey Georgie
Custom comment error page for Hey Georgie

See below for known issues with this method.

Known issues

I haven’t yet found out how to modify the default error messages in WordPress. So you will be stuck with the traditional ‘ERROR: please type a comment’ sort of messages. If you don’t like this, you can strip out a large chunk of our custom function and have this:

function custom_comment_error( $message, $title='', $args=array() ) {
  $errorTemplate = get_theme_root().'/'.get_template().'/commenterror.php';
  require_once( $errorTemplate );
  die();
}

This simply means you can include your own error message in your template and omit echo $message, but you should make your message text quite universal. By default, WordPress’ error messages know whether only the comment field has been left blank, or if the name or email fields have been left blank.

Another known issue is that some anti-spam plugins use the wp_die function, so their content (captchas, etc.) may appear in the content of your template if you use echo $message. Be wary of that – and by the same token, if you choose to omit the default error messaging and use your own template, your anti-spam plugin may not work.

Comments on this post

AH! This is so cool and sleek and nice and beautiful.

I’ll have to play with it sometime. <3

Awesome, thank you for haring this! :D

Ooops. I can’t spell. :'(

Thanks for this. It’s much easier to follow than other tutorials I’ve seen! :)

Doesn’t seem to be active. Just pressed the comment button without completing any info, and the same old white box wp error came up.