Open Source Code

WordPress Custom DB Error

Overview

Author
FWD:labs and the author of the custom database error page tutorial at digwp.com

Requirements
WordPress 4.9+

Version (Last Updated)
1.01 (2017-11-17 09:09:50)

License

The MIT License (MIT)

Copyright (c) 2016 FWD:labs

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Instructions

1. Make a file called db-error.php and use the following code.
2. Change the default configurations.
3. Upload db-error.php to your wp-content folder.

Code

<?php

// START CONFIG

$pageTitle = "503 Service Temporarily Unavailable"; // Name of web page in <title></title> tag

$contentTitle = ""; // Name of website
$contentEmail = ""; // E-mail address for customers/visitors to see
$contentMessage = <<<EOD
Sorry, we are currently experiencing technical issues.<br/><br/>
Please reload the page in a few moments and you should be on your way again.<br/><br/>
You can also try visiting the <a href="/">home page</a>
EOD;
if ($contentEmail <> "") {
	$contentMessage .= " or contact us ($email) about the error";
}
$contentMessage .= ".";

$debugName = ""; // Name associated with debug
$debugEmail = ""; // E-mail address for debug receipts

// STOP CONFIG

header('HTTP/1.1 503 Service Temporarily Unavailable');
header('Status: 503 Service Temporarily Unavailable');
header('Retry-After: 3600'); // 1 hour = 3600 seconds

// Function to get the customer/visitor IP address
if (!function_exists('get_client_ip')) {
	function get_client_ip() {
    $ipaddress = '';
    if ($_SERVER['HTTP_CLIENT_IP'])
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    else if($_SERVER['HTTP_X_FORWARDED_FOR'])
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    else if($_SERVER['HTTP_X_FORWARDED'])
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    else if($_SERVER['HTTP_FORWARDED_FOR'])
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    else if($_SERVER['HTTP_FORWARDED'])
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    else if($_SERVER['REMOTE_ADDR'])
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    else
        $ipaddress = 'UNKNOWN';
    return $ipaddress;
	}
}

// if ($_SERVER['REQUEST_URI'] != "/wp-content/db-error.php") {
$mailto = "$debugName <$debugEmail>";
$mailfrom = "$debugName <$debugEmail>";
$headers  = "From: ".$mailfrom."\r\n"."X-Mailer: PHP/".phpversion()."\r\n"."X-Priority: 1 (High)";
$message  = "Server displayed a 503 error";
$message .= " at http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$message .= " via a user at ".get_client_ip();
$subject  = "503 error at ".$_SERVER['SERVER_NAME'];
mail($mailto,$subject,$message,$headers);
// }

?>
<!DOCTYPE HTML>
<html dir="ltr" lang="en-US">
<head>
<title><?php echo $pageTitle; ?></title>
<style type="text/css">
html, body, #doc {
    margin: 0;
    padding: 0;
    height: 100%;
    width: 100%;
	}
#doc {
    display: table;
	}
#bd {
    display: table-cell;
    margin: 0;
    padding: 0;
    text-align: center;
    vertical-align: middle;
	}
#content {
	font-family:Helvetica, Arial, serif;
    height: 320px;
    margin: auto;
    text-align:left;
    width: 320px;
	}
</style>
</head>
<body>

<div id="doc">
<div id="bd">
<div id="content">

<h1><?php echo $contentTitle; ?></h1>

<p><?php echo $contentMessage; ?></p>

</div>
</div>
</div>

</body>
</html>

Changelog

2017-11-17: Fixed a typo in the default $contentMessage

Feedback

Questions, comments, concerns or ideas about this? Please let us know.

Twitter   Facebook   Instagram