Skip to content

Instantly share code, notes, and snippets.

@VR51
Last active October 11, 2023 10:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VR51/9b0632eadc24e7a869d3bfe0ed633efd to your computer and use it in GitHub Desktop.
Save VR51/9b0632eadc24e7a869d3bfe0ed633efd to your computer and use it in GitHub Desktop.
WordPress Custom PHP Error Report
<?php
header('HTTP/1.1 503 Service Temporarily Unavailable');
header('Status: 503 Service Temporarily Unavailable');
header('Retry-After: 60'); // 1 hour = 3600 seconds
/**
* WordPress Custom PHP Error Report
*
* Drop this file into wp-content.
* When a PHP error occurs a stacktrace will be sent to the email address congifured below here.
* Configure the email addresses on lines 27 ($to -- the recipient's address) and 28 ($from -- the from address, which should be configured on the server this script is stored on).
*
* 1. This script shows a friendly error message to visitors when fatal PHP errors occur. The message can be configured near the bottom of this script.
* 2. This script sends a report to the $to address when a fatal PHP error occurs on the monitored website.
*
* Copyright 2022 Lee Hodson, VR51, WP Service Masters
*
* Licence: GPL2 https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html
*/
// Email Error Log to Support User
/**
* CONFIGS
***/
$to = "info@example.com"; // Configure me
$from = "info@example.com"; // Configure me. Use an email address that is configured on this server.
// Error messages email styles
$style_h = 'style="margin-bottom: 15px;"'; // h# tags
$style_p = 'style="margin-bottom: 15px;"'; // p tags
$style_div='style="color: #000; background-image: radial-gradient(circle, lightskyblue, white);padding: 5px;"'; // div tags
// Public Message
$host = $_SERVER['HTTP_HOST']; // This probably should not be edited but if you really want to...
$public_message_header = "<h1>$host is temporarily unavailable for maintenance. Please refresh your browser in 60 seconds.</h1>";
$public_message_content = ""; // Is this really needed? Use HTML
/** END CONFS ***/
$subject = "WordPress PHP Error";
$uri = $_SERVER['REQUEST_URI'];
$e = new \Exception;
$trace = $e->getTraceAsString();
/* PHP Error Types */
$etype = array(
E_ERROR => "E_ERROR",
E_WARNING => "E_WARNING",
E_PARSE => "E_PARSE",
E_NOTICE => "E_NOTICE",
E_CORE_ERROR => "E_CORE_ERROR",
E_CORE_WARNING => "E_CORE_WARNING",
E_COMPILE_ERROR => "E_COMPILE_ERROR",
E_COMPILE_WARNING => "E_COMPILE_WARNING",
E_USER_ERROR => "E_USER_ERROR",
E_USER_WARNING => "E_USER_WARNING",
E_USER_NOTICE => "E_USER_NOTICE",
E_STRICT => "E_STRICT",
E_RECOVERABLE_ERROR => "E_RECOVERABLE_ERROR",
E_DEPRECATED => "E_DEPRECATED",
E_USER_DEPRECATED => "E_USER_DEPRECATED",
E_ALL => "E_ALL"
);
/* END PHP Error Types */
ob_start();
$find = array( '( ', ' [', ' )' );
$replace = array( '<br />(', '<br />&nbsp;&nbsp;[', '<br />)' );
$style_h = 'style="margin-bottom: 15px;"';
$style_p = 'style="margin-bottom: 15px;"';
$style_div='style="color: #000; background-image: radial-gradient(circle, lightskyblue, white);padding: 5px;"';
echo "<h2 $style_h >ERROR MESSAGE</h2>";
$err = error_get_last();
$type = $err['type'];
echo "<p><strong>Type:</strong> $etype[$type]</p>";
$err = "<p $style_p ><div $style_div >" . print_r( $err, true ) . "</div></p>";
echo str_replace( $find, $replace, $err );
echo "<h2 $style_h >SERVER</h2>";
$err = "<p $style_p ><div $style_div >" . print_r( $_SERVER, true ) . "</div></p>";
echo str_replace( $find, $replace, $err );
echo "<h2 $style_h >POST</h2>";
$err = "<p $style_p ><div $style_div >" . print_r( $_POST, true ) . "</div></p>";
echo str_replace( $find, $replace, $err );
echo "<h2 $style_h >GET</h2>";
$err = "<p $style_p ><div $style_div >" . print_r( $_GET, true ) . "</div></p>";
echo str_replace( $find, $replace, $err );
echo "<h2 $style_h >GLOBAL KEYS</h2>";
$err = "<p $style_p ><div $style_div >" . print_r( array_keys(get_defined_vars()), true ) . "</div></p>";
echo str_replace( $find, $replace, $err );
echo "<h2 $style_h >GLOBAL VALUES</h2>";
$err = "<p $style_p ><div $style_div >" . print_r( get_defined_vars(), true ) . "</div></p>";
echo str_replace( $find, $replace, $err );
echo "<h2 $style_h >QUERIES</h2>";
global $wpdb;
$err = "<p $style_p ><div $style_div >" . print_r( $wpdb->queries, true ) . "</div></p>";
echo str_replace( $find, $replace, $err );
$wperr = ob_get_clean();
$msg = "<h1>$host</h1><h2>A PHP $etype[$type] error has occurred on $host</h2><p><strong>Error triggered on:</strong> <a href='https://$host$uri'>$host$uri</a></p><p><strong>WP Stacktrace:</strong> $trace</p><strong><hr /></strong><h1>Error Report</h1>$wperr";
$headers = "MIME-Version: 1.0" . "\r\n" . "Content-type: text/html; charset=iso-8859-1" . "\r\n" . "From: $from";
mail($to,$subject,$msg,$headers);
?>
<!-- Display Nice Error Page to Affected Visitor -->
<!DOCTYPE HTML>
<html dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width">
<title>Maintenance Page</title>
<style type="text/css">
html {
box-sizing: border-box;
display: grid;
align-items: center;
justify-content: center;
background: lightskyblue;
background-image: radial-gradient(circle, lightskyblue, white);
box-sizing: border-box;
}
body {
justify-content: center;
background: #fff;
border: 1px solid #ccd0d4;
color: #444;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
margin: 2em auto;
padding: 1em 2em;
max-width: 700px;
-webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, .04);
box-shadow: 0 1px 1px rgba(0, 0, 0, .04);
}
h1 {
border-bottom: 1px solid #dadada;
clear: both;
color: #666;
font-size: 24px;
margin: 30px 0 0 0;
padding: 0;
padding-bottom: 7px;
}
p {
}
#error-page {
margin-top: 50px;
}
#error-page p,
#error-page .wp-die-message {
font-size: 14px;
line-height: 1.5;
margin: 25px 0 20px;
}
#error-page code {
font-family: Consolas, Monaco, monospace;
}
ul li {
margin-bottom: 10px;
font-size: 14px ;
}
a {
color: #0073aa;
}
a:hover,
a:active {
color: #006799;
}
a:focus {
color: #124964;
-webkit-box-shadow:
0 0 0 1px #5b9dd9,
0 0 2px 1px rgba(30, 140, 190, 0.8);
box-shadow:
0 0 0 1px #5b9dd9,
0 0 2px 1px rgba(30, 140, 190, 0.8);
outline: none;
}
.button {
background: #f3f5f6;
border: 1px solid #016087;
color: #016087;
display: inline-block;
text-decoration: none;
font-size: 13px;
line-height: 2;
height: 28px;
margin: 0;
padding: 0 10px 1px;
cursor: pointer;
-webkit-border-radius: 3px;
-webkit-appearance: none;
border-radius: 3px;
white-space: nowrap;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
vertical-align: top;
}
.button.button-large {
line-height: 2.30769231;
min-height: 32px;
padding: 0 12px;
}
.button:hover,
.button:focus {
background: #f1f1f1;
}
.button:focus {
background: #f3f5f6;
border-color: #007cba;
-webkit-box-shadow: 0 0 0 1px #007cba;
box-shadow: 0 0 0 1px #007cba;
color: #016087;
outline: 2px solid transparent;
outline-offset: 0;
}
.button:active {
background: #f3f5f6;
border-color: #7e8993;
-webkit-box-shadow: none;
box-shadow: none;
}
</style>
</head>
<body id="error-page">
<div class="wp-die-message">
<?php echo $public_message_header ?>
<?php echo $public_message_content ?>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment