Custom WordPress database error page

WordPress[1] relies on a database to perform all its magic. This is a good way of doing things, as it means that all the posts are stored as database records, with the pages you see being created on the fly rather than there being many thousands of prebuilt HTML files. Having all those files wouldn’t be a space problem, but they’d need to be rebuilt every time I made the slightest change to the design or layout of the site, which would make the server quite unhappy. However, it does mean that there’s one more thing to go wrong, and occasionally it does just that. Sometimes it’s the database server having a bad day, being rebooted or whatever, and just occasionally it’s a problem with my site that’s making the database unhappy. Either way, if WordPress can’t see its database, it can’t do anything other than throw up its hands and say “it’s broken”.

Until the release of WordPress 2.3.2, an error page similar to this was displayed.

Old Error Page

Old Error Page

Now there are a number of problems with this. It presents information that is only of use to the person running the site – it babbles on about the wp-config.php file, it suggests that the database server might have gone for a walk and suggests a visit to the WordPress support forums. Now if it’s your site that’s broken, that’s good advice. But if you’re trying to visit a site and you see that, it’s not really useful information. Indeed, as it mentions the name of the database server, it’s the kind of thing that is considered to be a bit of a security no-no. It’s not generally considered a good idea to give the naughty people any more information than you absolutely need to.

Another problem with the message is that it says “WordPress” on it in large friendly letters, and doesn’t identify the actual site that has the problem. So what happens is that a site becomes unexpectedly popular (maybe it gets a mention on Digg or some such place), the little database server can’t take the load and falls over, and lots of people see a message telling them that WordPress is broken. And as lots of people don’t always think things through, they get the idea that this means that the hosted Matt had chosen a different name for the hosted service, but it’s a bit late for that now…

So, in response to these problems, WordPress now presents this much more sensible error message. It’s short and to the point, doesn’t reveal any potentially dangerous details, and doesn’t suggest that WordPress itself is borked.

New Database Error Message

New Database Error Message

What’s that? You don’t like it? You think it’s maybe a little too terse? Well, there’s some good news for you. Previously, if you wanted to replace the standard error page, you had to edit core WordPress files. Now that’s not all that hard (other people have done it and documented the process), but it’s a bit annoying to have to redo your work every time there’s a WordPress update. Some of my own tweaks used to require a little similar tweaking, and I had to keep a changelog file so I knew what to redo every time. As WordPress and its plugins developed, I was able to abandon such unsavoury activities, and I’m much happier for it. Anyway, all you have to do now is create a text file called db-error.php and save it in the wp-content directory on your server. Then, if your database server gets upset, your visitors will see your error page rather than a generic WordPress one. People will know that they’ve reached the right place, nobody will see any information they don’t need to see, WordPress won’t get the blame for your broken database server, and everyone will be happy. Or as happy as you can be when your database isn’t talking to you.

I’ve created a custom page which matches the current look of the site. It won’t automagically change if I change the layout, but as it uses the same stylesheet as the live site, it will keep up with changes to the colour scheme.

Custom Error Page

Custom Error Page

Rather than creating this from scratch, I based it on the page that appears when a visitor tries to go to a page that doesn’t exist on the site. I brought up that page, then using Firefox, I right-clicked and told it to display the page source in my favourite text editor (TextPad on Windows. Not sure about Macs yet). I then chopped out the bits I didn’t want, like the sidebar, tons of JavaScript and other bits that are needed for normal pages, and put some suitable text in the middle of the page. I saved it as db-error.php and uploaded it.

I gave it a quick test by uploading a slightly edited version of my wp-config.php file, and it works!

While WordPress point releases are normally just bug fixes and security updates, this one actually adds a useful feature. Nice work!

[1] That’s the software I use to run Losing it[1] for those who haven’t been paying attention, or who aren’t usually bothered by such irrelevant geeky details
[2] And lots of support forum confusion