"Critical Error on This Website" on WP: Notify and Prevent

James Parsons by James Parsons Updated Feb 6th, 2024

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());


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!

Related Code Entries

Written by James Parsons

James Parsons is the founder and CEO of Content Powered, a premier content marketing agency that leverages nearly two decades of his experience in content marketing to drive business growth. Renowned for founding and scaling multi-million dollar eCommerce businesses through strategic content marketing, James has become a trusted voice in the industry, sharing his insights in Search Engine Watch, Search Engine Journal, Forbes, Entrepreneur, Inc, and other leading publications. His background encompasses key roles across various agencies, contributing to the content strategies of major brands like eBay and Expedia. James's expertise spans SEO, conversion rate optimization, and effective content strategies, making him a pivotal figure in the industry.