Custom Error Message

Following on from a previous post regarding the DON’T PANIC ethos required of every webmaster when a site goes down, a little revelation regarding the actual problem and an alternative alert for your readers who would otherwise seen an incongruous WordPress error message.

Okay, so there was a database (MySQL) crash at the host for the site in question, which meant all blogging functionality collapsed, as the site hangs on this functionality to render itself it had nowhere to go but to display a little file called wp-db.php This file resides in the WordPress blog’s wp-includes folder, so here’s what to do to make it more useful to your readers and to modify it so that it sends you an email alert.

  1. Make a backup of wp-db.php
  2. Make another backup of wp-db.php
  3. Unearth the URL of your site’s logo, make sure it’s no wider than about 450 pixels
  4. If you have to create a new logo to comply with this size restriction upload it to your images folder and note the URL
  5. Open wp-db.php in your favorite programmer’s text editor, not Notepad or Word
  6. Scroll to the bottom of the wp-db.php and replace the URL “” with the URL to your logo “”.
  7. Save the wp-db.php file and upload it to your wp-includes folder
  8. Check to see what effect this had by opening the file “, you should see your logo, along with a load of text about database and login errors.
  9. Next re-open wp-db.php in your text editor and scroll down to the line containing ” function bail($message) { // Just wraps errors in a nice header and footer” or something similar, it’s above a line “if ( !$this->show_errors )” and will be at approximately line 307-335 depending on your installation of WordPress.
  10. Paste the following in between those two lines

    “/* custom error page hack
    emails error */
    mail(‘’, ‘Sciencebase MySQL Error’, $error);
    /* end custom error page hack */”

    Editing the dummy email address for your real email address.

  11. Save wp-db.php, upload it once more and check that it is working
  12. Next is an optional step, if you want rid of all those database error remarks and would like to include your own message to your readers then scroll to the section beginning ” $this->bail(“”, it’s round about line 50. You can overwrite the gubbins you see there with a personal message and even include links to a mirror site or alternative webs to which you wish to direct your error-stricken visitors. Just remember to use single quote marks in the anchors.
  13. Save wp-db.php, upload it once more and check that it is working properly
  14. Remember, if you upgrade your WordPress installation, you will have to edit the wp-db.php file again to enable these features.

So, with all that in place, should a MySQL error occur and your site be unable to display its WordPress components, your readers will not see a nasty default error, but will see your logo and custom error message, and more to the point you will receive an email alerting you to the problem so that you can contact your host’s technical support and get things fixed.

There are other more sophisticated methods of overcoming the problem of the default error which provided some inspiration for this post, these can be found at 5thirtyone and allforces, to which kudos.

One additional loose end to tie up, exclude this file from search engine accesses in your robots.txt file (or with a meta noindex) to prevent false positives.

Author: David Bradley

Freelance science journalist, author of Deceived Wisdom. Photographer and musician.