Custom WordPress Database Error

UPDATE 12/31: As of WordPress version 2.3.2, users can now easily replace their Database error using wp-content/db-error.php as their error template.

If you’re a longtime WordPress user, you’re probably aware of the Database Error page which rears its not so shiny face whenever your MySQL server decides to take a bathroom break. There’s no shame in enjoying the default error page which comes prepackaged with WordPress. It simple, effective, plain, did I say plain yet? Yes it’s plain.

After noticing that 5ThirtyOne was dead in the water due to server downtime, I decided the default error page had to go.

custom wordpress error page

Fortunately, swapping the default is quite simple and something that I highly recommend to any and all WordPress users. There’s a certain sense of “professionalism” to WordPress sites which customize the smallest details to match their entire site through and through.

Where is the database error template served?

WordPress 2.0+ users can find the necessary lines to edit within ‘/wp-includes/wp-db.php’. Beginning immediately below line 307, paste the following:

/* custom error page hack
shows a custom error page and emails error instead of
showing the default wordpress database error page */
include('wp-content/themes/themename/dbase-error.php');
$error = ( !$this->show_errors ) ? '' : $this->show_errors ;
mail('admin@domain.com', 'WordPress Error', $error);
die;
/* end custom error page hack */

For a hand-holding walk-through of each snippet of code, head on over to All Forces where Melvin breaks down the little stuff. The most important details should be recognizeable above: a) the path to your custom template titled dbase-error.php, and b) the email address that WordPress should notify in case of emergency.

A simple template to get you started

Once you’ve made the appropriate edits within wp-db.php, you can now move on to creating a suitable error page to match the look and feel of your site. Below I’ve shared a little sample of code to get you started. Feel free to download a physical copy of dbase-error.php for upload.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head profile="http://gmpg.org/xfn/11">
<title>My server crashed, please call the next of kin</title>
<style type="text/css">
body {
font-family: arial, verdana, sans-serif;
font-size: 62.5%;
line-height: 1.7em;
background: #fff;
color: #333;
text-align: center;
}
a {
text-decoration: none;
outline: none;
}
#error_wrapper {
font-size: 1.3em;
margin: 2em auto 0;
text-align: left;
width: 333px;
}
.content_wrapper {
padding: 1em 0.7em 0;
}
</style>
</head>
<body>
<div id="error_wrapper">
<div class="content_wrapper">
<p>It looks as though my server has taken an unsupervised leave of absence. I may not know of its whereabouts so would you be so kind as to notify me via <a href="mailto:admin@domain.com" title="Send me an email">email</a>?</p>
</div>
</div>
</body>
</html>

I’ve chosen to embed the CSS for the sake of convenience. If your error page begins to get a little disorganized, moving your stylesheet to an external source may be a more suitable route. Make note that all references to files within WordPress must be absolute paths.

Show me yours

Now that you have a clean canvas to work with, show me what your database error message will be. Being that errors are few and far between, I’ve pasted mine below:

Unfortunately, it seems that 5ThirtyOne is suffering from a database error which prevents this WordPress driven site from delivering the content which you so desperately seek.

Rest assured that the lemmings living within the servers are hastily working to restore the MySQL database making everyone – especially me – a little less irritated.

If I broke my own site on accident, I really didn’t mean to. Change is good though right?

Let’s try to be original. If there’s one thing worse than copying someones ‘About‘ page, it’s copying someones error page. Drop your renditions in the comments.

Discuss - 22 Comments

  1. [...] et surtout pas tellement professionnel… A force de recherche sur Internet je suis tombé sur un hack de wordpress très pratique qui en plus d’afficher une page d’erreur personnalisé permet de vous envoyer un [...]