Geeky Nuggets

Wordpress 404 notification by email

January 22, 2011 | 2 Minute Read

I couldn’t find something that did just what I wanted and no more: send me an email when someone gets a 404 error on my blog. It seems pretty simple, and it really is. More than that actually. It’s problably the simplest plugin I’ve ever written.

Check it out:

function email_error(){
    global $wp_query;
    if ($wp_query-&gt;is_404){
add_action('get_header', 'email_error');

This is the main part of the code, the one gets executed everytime the get_header hook is called. It basically just checks the $wp_query object to see if the current url gives a 404 error and then passes that url to the email_admin function

function email_admin($location){
    $email = get_option('admin_email');
    $headers  = "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/plain; charset=UTF-8\r\n";
    $headers .= 'From: "' . $name . '" &lt;' .$email. "&gt;\r\n";
    $subject='404 error in '.$name;
    $body='A 404 error occured at the following url: '.$_SERVER['SERVER_NAME'].$location;

This function sets some headers for the email, such as the sender’s name and email, and the subject of the message, and then sends the message. No error is shown if the message can’t be sent, as this would be displayed on the page.

UPDATE: Finally available on the wordpress plugin repository