Geeky Nuggets

Wordpress 404 notification by email

January 22, 2011 | 1 Minutes 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:

<?php
function email_error(){
    global $wp_query;
    $location=$_SERVER['REQUEST_URI'];
    if ($wp_query-&gt;is_404){
        email_admin($location);
    }
}
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

<?php
function email_admin($location){
    $name=get_option('blogname');
    $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;
    @mail($email,$subject,$body,$headers);
}
?>

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

Older posts

  • January 14, 2011

    Spanish characters on Qwerty keyboard for Ubuntu 10.10

    January 14, 2011 | 1 Minutes Read

    Set a configuration option to be able to easily type acendted character on Ubuntu 10.10.

  • January 14, 2011

    Python skype notifier for Ubuntu

    January 14, 2011 | 22 Minutes Read

    Learning python by building a semi-useful Skype instant message notifier.

  • January 12, 2011

    Compile custom kernel on Ubuntu 10.10

    January 12, 2011 | 3 Minutes Read

    My experience compiling a custom kernel from source, using Ubuntu 10.10 Maverick Meerkat, on a Sony Vaio F111 series.

  • May 19, 2010

    New version of the auto-tag plugin released!

    May 19, 2010 | 0 Minutes Read

    I finally release a new version for the auto-tag plugin. Hopefully this one sucks a bit less!!

  • January 02, 2010

    One app a month challenge

    January 02, 2010 | 1 Minutes Read

    To keep learning and progressing in my line of work, you have to be learning new things everyday. The evolution is so fast that if you don’t go forward, you are effectively going backwards.I need ideas of useful apps or libraries to include in this challenge.So, my challenge for 2010 will be to build one web app every month, and publish it here on the last day of each month. My schedule is pretty tight right now, so the apps will have to be rather quick to develop, but hopefuly it will help m...