WordPress db-error.php drop-in template
One of the more useful WordPress must-use plugins and drop-ins is the db-error.php
file, which you can create, customize, and upload to your site’s /wp-content/
directory. Upon doing so, the contents of the file will be displayed whenever the database is unavailable. This post provides a simple template for db-error.php
that you can customize and add to your own site.
<?php ob_start();
header('HTTP/1.1 503 Service Temporarily Unavailable');
header('Status: 503 Service Temporarily Unavailable');
header('Retry-After: 3600'); // 1 hour = 3600 seconds
mail("email@example.com", "Database Error", "There is a problem with teh database!" . $_SERVER['REMOTE_ADDR'], "From: Sir Montgomery Scott");
?><!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>503 Service Temporarily Unavailable</title>
</head>
<body>
<h1>Service Temporarily Unavailable</h1>
<p>The site is currently being updated. Please try again later.<p>
<hr>
<address>Web Server at example.com</address>
</body>
</html>
Usage
- Change the
email@example.com
to your own email address - Optionally edit the other parameters of the
mail()
function - Create a file named
db-error.php
- Inside of the file, add the previous code
- Upload the file to your site’s
/wp-content/
directory - Done!
This simple template is meant to be customized! Functionality currently includes just the basics:
- Sets appropriate 503 headers
- Sets retry-after header
- Sends you an email alert
- Displays a simple message
A great way to improve the utility of this simple drop-in plugin is to integrate better variable reporting. For example, you can integrate the variable-grabbing technique provided by my 404 alert script to include more visitor information in the email alert.