Get email alerts about 404 errors on your WordPress site

View source (.md)

Ever wanted to get email alerts about 404 errors on your WordPress site?

Jeff Starr over at WP Mix posted a snippet yesterday for doing just that. And I, having a little time on my hands, decided to give it the ol' OOP-once-over.

The original WP Mix post called for dropping the entire snippet into the top of your WordPress theme's 404.php file, but I generally don't like to clutter up my template files with extra non-theme stuff. So I converted it into a class which can then be easily instantiated in with these two lines:

[php] if ( class_exists( 'Clean_404_Email' ) ) new Clean_404_Email; [/php]

Not bad, considering the original snippet called for dropping in 83 lines of extra code!

I also converted the email format to a use a table, just so it's a little more orderly about it.

Many users take advantage of freely-available tools like Google's Webmaster Tools or other services to track a site's 404 errors. This class gives you the short and sweet of it, and there's no waiting around. If somebody gets a 404 on your site, WordPress will email you on the spot.

The gist is available on GitHub and also embedded below.

Photo used under CC. Photo by Jeremy Keith (adactio/Flickr)