Send Email Alerts to Administrator When a 404 Page Occurs in WordPress
A 404 error page can negatively impact both user experience and SEO. This article provides a snippet of PHP code that allows WordPress to automatically send an alert email to the site administrator whenever a 404 page is encountered. The email includes detailed information such as site and theme info, the requested URL, the referring URL, and more—helping administrators quickly identify and fix broken links to improve site reliability and SEO performance.
Why Monitor 404 Pages?
Over time, it’s inevitable that a site will accumulate some broken links, resulting in 404 errors. These errors can leave a poor impression on users and cause search engines to downgrade your site’s credibility. If you can detect and address 404 errors promptly, it reflects well on your site’s reliability and contributes positively to SEO.
The following code snippet, when added to your theme’s functions.php
file, will send an email notification to the administrator each time a 404 page is triggered.
Code Snippet: Send Email on 404 Error
// Site Info
$blog = get_bloginfo('name');
$site = get_bloginfo('url') . '/';
$email = get_bloginfo('admin_email');
// Theme Info
if (!empty($_COOKIE['nkthemeswitch' . COOKIEHASH])) {
$theme = clean($_COOKIE['nkthemeswitch' . COOKIEHASH]);
} else {
$theme_data = wp_get_theme();
$theme = clean($theme_data->Name);
}
// Referrer
$referer = isset($_SERVER['HTTP_REFERER']) ? clean($_SERVER['HTTP_REFERER']) : 'undefined';
// Request URI
$request = (isset($_SERVER['REQUEST_URI']) && isset($_SERVER['HTTP_HOST'])) ?
clean('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']) : 'undefined';
// Query String
$string = isset($_SERVER['QUERY_STRING']) ? clean($_SERVER['QUERY_STRING']) : 'undefined';
// IP Address
$address = isset($_SERVER['REMOTE_ADDR']) ? clean($_SERVER['REMOTE_ADDR']) : 'undefined';
// User Agent
$agent = isset($_SERVER['HTTP_USER_AGENT']) ? clean($_SERVER['HTTP_USER_AGENT']) : 'undefined';
// Remote Identity
$remote = isset($_SERVER['REMOTE_IDENT']) ? clean($_SERVER['REMOTE_IDENT']) : 'undefined';
// Timestamp
$time = clean(date('F jS Y, h:ia', time()));
// Sanitization Function
function clean($string) {
$string = trim($string);
$string = htmlentities($string, ENT_QUOTES);
$string = str_replace("\n", '', $string);
if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {
$string = stripslashes($string);
}
return $string;
}
// Compose the message
$message =
"TIME: " . $time . "\n" .
"*404 URL: " . $request . "\n" .
"SITE: " . $site . "\n" .
"THEME: " . $theme . "\n" .
"REFERRER: " . $referer . "\n" .
"QUERY STRING: " . $string . "\n" .
"REMOTE ADDRESS: " . $address . "\n" .
"REMOTE IDENTITY: " . $remote . "\n" .
"USER AGENT: " . $agent . "\n\n";
// Send the email
mail($email, '404 Alert: ' . $blog . ' [' . $theme . ']', $message, 'From: ' . $email);
How to Use
- Open your WordPress theme folder.
- Edit the
functions.php
file. - Paste the above code at the bottom of the file.
- Save and upload the updated file if needed.