How to Email 404 Error to WordPress Admin


Email 404 error when your browser is not able to locate a URL on your web server? Is it not interesting?  As a vigilant Administrator and SEO guy you want to remove all these types of errors from your website. What you do to find out all 404 errors on your website ?

Dig deep into your website’s log or want a smart solution that automatically inform you whenever your server is not able to locate any page/post/media on your site!

My website’s 404.php file is able to provide its reader some more option instead of simple sorry message, but like i said  I want more fine control on my website.

email 404 error

Just open your 404.php file and copy paste this code on the top, if you do not have any 404.php file, then go ahead and create one for your website.

Email 404 Error Code

<?php // WP 404 ALERTS @ http://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"); 
?>

Now my 404.php file looks like this- Just see how and where I have added this whole code in my existing file.

<?php // WP 404 ALERTS @ http://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");

?>

<?php get_header();?>
<!-- Main component for body of this document -->
<div style="margin-top:65px;">
<div>
<div >
<h1> Oops! You are so smart. </h1>
<hr/>
<p><?php _e( 'Apologies, but the page you requested could not be found. Perhaps searching will help.', 'VideoSh' ); ?></p>
<div>
<div>
<?php category_listing(get_theme_option('category_one')); ?>
</div>
<div>
<?php category_listing(get_theme_option('category_two')); ?>
</div>
<div>
<?php category_listing(get_theme_option('category_three')); ?>
</div>
<div>
<?php category_listing(get_theme_option('category_four')); ?>
</div>
<div>
<?php category_listing(get_theme_option('category_five')); ?>
</div>
<div>
<?php category_listing(get_theme_option('category_six')); ?>
</div>
</div>

</div>
<?php get_sidebar();?>
</div>
</div> <!-- /container body -->
<!---------navigation page number start here ------------->
<div></div>
<hr/>
<?php get_footer();?>

A big thanks to wp-mix for this wonderful emailing tricks.
Though my WordPress theme has a special 404.php file that not only display the most popular post from six user defined categories but also send me an email alert. This way i am able to reduce bounce rate on my website as well as able to remove these soft errors.




Suggested Similar Articles

6 Brilliant Comments - Join Discussion Now!

  1. Although Great sharing,But i am not aware of PHP,so it looks little difficult to understand it.
    I know if i am able to get it properly,then it will be very useful to me…
    great tricks to be shared…:)

    • rakesh says:

      If you want your 404.php file like this just send me your 404.php file available in your theme folder,I can do this changes for you. ;)

  2. Shiv Sharma says:

    Great tip Rakesh on WordPress 404 Error page. I am new to the blogging field and will definitely give a try to this very useful tip.

    • Having such a code integrated in the template file is really a must. Of course, I would like to prevent all such kind of errors from my site! Thanks for that lovely tutorial!

    • rakesh says:

      Hi Vanessa, If you would like to implement this type of code in your theme, We can do this for you free of cost. Just ping us :)

  3. Bishnu Dev says:

    Bunch of codes :p feeling difficult to understand :)

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>