"Critical Error on This Website" on WP: Notify and Prevent
This is another fun one! Today I logged into check the latest search engine and SEO news as I usually do, and I see this on Search Engine Watch:
What's worse, it's been like this all day. I'm surprised nobody's fixed it by now when hundreds of thousands of people are seeing this error and leaving.
Needless to say, this situation sucks, and it's happened to us all! This is just how WordPress handles things. If a plugin has an issue after an automatic update, it breaks your entire site. They don't disable the plugin for security reasons; after all, if it's your security plugin that happened to malfunction, this would disable your security and your site would be easier to compromise. To WordPress, it's safer to just take your site down - even if the plugin isn't mission critical.
I don't think it's the best way of handling these situations, and it's better to be proactive about these things so you don't end up like Search Engine Watch today.
I set out to create a script that will keep a look out for these critical PHP errors and if it finds a plugin with a critical error, it will alert you (the admin) so that you can fix it quickly.
Note: Thoroughly test this in a non-production environment before you consider using this on a live site.
/*
* Notify me when a plugin just broke my site
*/
function notify_admin_of_plugin_error($pluginDir, $errorType, $message, $file, $line) {
$lastSent = get_option('my_custom_error_last_sent_' . $pluginDir);
$currentTime = time();
if (!$lastSent || $currentTime - $lastSent >= 3600) {
update_option('my_custom_error_last_sent_' . $pluginDir, $currentTime);
$subject = "Critical Plugin Error: " . $pluginDir;
$body = "A critical error occurred in the plugin: {$pluginDir}\n";
$body .= "Error Type: {$errorType}\n";
$body .= "Error Message: {$message}\n";
$body .= "File: {$file}\n";
$body .= "Line: {$line}\n";
$body .= "Time: " . date("Y-m-d H:i:s", $currentTime);
wp_mail(get_option('admin_email'), $subject, $body);
error_log("Notified admin of critical error in plugin: {$pluginDir}");
}
}
function my_custom_error_handler($severity, $message, $file, $line) {
if (!in_array($severity, [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR])) {
return false;
}
if (strpos($file, '/wp-content/plugins/') !== false) {
$start = strpos($file, '/plugins/') + 9;
$end = strpos($file, '/', $start);
$pluginDir = substr($file, $start, $end - $start);
notify_admin_of_plugin_error($pluginDir, "Error", $message, $file, $line);
}
return false;
}
function my_custom_exception_handler($exception) {
$file = $exception->getFile();
if (strpos($file, '/wp-content/plugins/') !== false) {
$start = strpos($file, '/plugins/') + 9;
$end = strpos($file, '/', $start);
$pluginDir = substr($file, $start, $end - $start);
notify_admin_of_plugin_error($pluginDir, "Exception", $exception->getMessage(), $file, $exception->getLine());
}
}
set_error_handler('my_custom_error_handler');
set_exception_handler('my_custom_exception_handler');
How to install
To install, save this code as a .php file (example: notify-on-critical-error.php) and then upload that file to the following directory via FTP or a file manager plugin:
- /wp-content/mu-plugins/
Why the /mu-plugins/ folder?
Well, if you have a real site-breaking issue, there's a real chance that execution of your code will stop before this code even has a chance to run. And that kind of defeats the purpose, right?
What's cool is that when you put this code in the /mu-plugins/ directory, it will run before all of your other plugins and theme code. This establishes a much more reliable mechanism to react to errors.
How does it notify me?
This is configured to message the admin email (the one set in WordPress > Settings > General > Administration Email Address) when something breaks.
If you wanted to, you could even modify this pretty easily to send you a SMS message for even faster and more urgent notifications. After all, if your site goes down when you're out
This script is configured to send a message no more than once per hour, so you won't get hit with a thousand emails over and over. But, don't wait forever, or those notices will stack up - this is designed for critical, site-breaking errors and emergency notifications. of the office, an email may not do you much good unless you happen to be checking your inbox.
What versions of WordPress and PHP does this work with?
This should work with most modern versions of WordPress.
The error "There has been a critical error on this website." was introduced in WordPress Version 5.2 on May 7, 2019 - so since you're reading this and you're trying to prevent these errors, I can safely assume your WordPress installation is newer than that!
This is also configured to work with various versions of PHP. Before PHP 7, errors were represented by simple error codes which limited error handling. PHP 7 introduced a hierarchy of throwable exceptions and errors for more sophisticated error handling. This code should satisfy both as it has two different error handlers; one legacy, one modern.
Closing thoughts
What do you think? How do you currently handle and prevent the "There has been a critical error on this website." WordPress error? Please let me know in the comments!
October 09, 2024
Hi there,
The idea about a script to catch PHP errors and notify admins faster seems really smart. I actually had the same kind of problem when a plugin update crashed my site. Setting up email alerts has kept me up-to-date way faster! Thanks a bunch for the coding tips.
October 10, 2024
Hey Elvin!
I'm really happy that you found the script idea helpful! Those plugin errors can definitely be a real headache sometimes. Email alerts can really help keep you updated. If you want to take a look at anything else just ask! 😉