Let WordPress email you about 404 pages

Everyone has experienced a 404, aka “Not Found” code. When a visitor comes to your site and gets the 404, it means he was able to communicate with the server, but the content he was looking for was nowhere to be found. It’s a broken link and no one likes having those.

So, if you’re running your own WordPress website, you should deal with 404 error codes. There hundreds of plugins which can do that for you – whether it’s a plugin specifically designed to deal with those pages, a plugin which will help you create a template or an SEO plugin which will somehow redirect those pages.

But if you want a quick solution, you might find this code helpful. To make it work, you only have to copy and paste the code on top of your 404.php file:

<?php // WP 404 ALERTS @ https://wp-mix.com/wordpress-404-email-alerts/

 

// set status

header("HTTP/1.1 404 Not Found");

header("Status: 404 Not Found");

 

// 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

if (isset($_SERVER['HTTP_REFERER'])) {

$referer = clean($_SERVER['HTTP_REFERER']);

} else {

$referer = "undefined";

}

// request URI

if (isset($_SERVER['REQUEST_URI']) && isset($_SERVER["HTTP_HOST"])) {

$request = clean('http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);

} else {

$request = "undefined";

}

// query string

if (isset($_SERVER['QUERY_STRING'])) {

$string = clean($_SERVER['QUERY_STRING']);

} else {

$string = "undefined";

}

// IP address

if (isset($_SERVER['REMOTE_ADDR'])) {

$address = clean($_SERVER['REMOTE_ADDR']);

} else {

$address = "undefined";

}

// user agent

if (isset($_SERVER['HTTP_USER_AGENT'])) {

$agent = clean($_SERVER['HTTP_USER_AGENT']);

} else {

$agent = "undefined";

}

// identity

if (isset($_SERVER['REMOTE_IDENT'])) {

$remote = clean($_SERVER['REMOTE_IDENT']);

} else {

$remote = "undefined";

}

// log time

$time = clean(date("F jS Y, h:ia", time()));

 

// sanitize

function clean($string) {

$string = rtrim($string);

$string = ltrim($string);

$string = htmlentities($string, ENT_QUOTES);

$string = str_replace("\n", "<br>", $string);

 

if (get_magic_quotes_gpc()) {

$string = stripslashes($string);

}

return $string;

}

 

$message =

"TIME: "            . $time    . "\n" .

"*404: "            . $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\n";

mail($email, "404 Alert: " . $blog . " [" . $theme . "]", $message, "From: $email");

?>

Since the code will use email details from your admin profile, there is no editing required – copy and paste the code and start catching those 404 pages in mail. But if you want to specifiy another email address where the 404 errors will be delivered, you can do that by editing this line of code:

$email =’your-email@mail.com’;

If you want to customize the code, you should know that all variables are available for use on the page. Thanks to WP-Mix for the code. If you want your mails to be arranged into tables, the original code has been improved by Drew Jaynes.

After you have pasted the code and saved the changes, every 404 page will send you an email containing useful information about the error. Now you can address the particular 404 page and help your site in seconds.

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

Leave a Reply

Your email address will not be published. Required fields are marked *