WordPress – customize your database connection error message

Some time, your MySQL server die unexpectedly, you don’t have any plan for it and this is what you’ll get

error-establishing-connection-database

ugly huh ? i personally hate this error message a lot. so i decided to turn it to something better, make me feel comfortable and give me chances to fix this issue.

if you look at wp-includes/functions.php, line 2710, there’s a dead_db function


function dead_db() {
global $wpdb;

// Load custom DB error template, if present.
if ( file_exists( WP_CONTENT_DIR . '/db-error.php' ) ) {
require_once( WP_CONTENT_DIR . '/db-error.php' );
die();
}

// If installing or in the admin, provide the verbose message.
if ( defined('WP_INSTALLING') || defined('WP_ADMIN') )
wp_die($wpdb->error);

// Otherwise, be terse.
status_header( 500 );
nocache_headers();
header( 'Content-Type: text/html; charset=utf-8' );

wp_load_translations_early();
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"<?php if ( is_rtl() ) echo ' dir="rtl"'; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php _e( 'Database Error' ); ?></title>

</head>
<body>
<h1><?php _e( 'Error establishing a database connection' ); ?></h1>
</body>
</html>
<?php
die();
}

there it is, you can have a db error template for that ugly message. From here, what you need to do is creating your own message. this is mine :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Site under maintenance</title>
<style type="text/css">
body {
background: #3C78B5;
font-family: sans-serif;
}
.content {
position: fixed;
top: 35%;
left: 50%;
margin-top: -100px;
margin-left: -300px;
width: 600px;
border-radius: 5px;
padding: 20px;
background-color: #F8F8F6;
box-shadow: 0 0 5px #555555;
}
.para,.logo {
height : 100px;
display: table-cell;
vertical-align: middle;
text-align: center;
}
.para {
border-right: 1px solid #D1D1D2;
width: 50%;
}
.logo {
width: 40%;
padding-left: 15px;
}
</style>
</head>

<body>
<div>
<div>
This site is currently undergoing maintenance. Please check back shortly.
</div>
<div>
<img src="/blog-logo.png"/>
</div>
</div>
</body>

tadaaa!

custom db connection error message