WP-Mix

A fresh mix of code snippets and tutorials

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.

Update: for a more in-depth tutorial on this topic, check out my post over at DigWP.com: Create a Custom Database Error Page in WordPress.
<?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

  1. Change the email@example.com to your own email address
  2. Optionally edit the other parameters of the mail() function
  3. Create a file named db-error.php
  4. Inside of the file, add the previous code
  5. Upload the file to your site’s /wp-content/ directory
  6. 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.

Learn more

Digging Into WordPressWordPress Themes In DepthWizard’s SQL Recipes for WordPress