Get email alerts about 404 errors on your WordPress site

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:

if ( class_exists( 'Clean_404_Email' ) )
	new Clean_404_Email;

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)

Published by

Drew Jaynes

Drew is a former Core Developer for the WordPress open source project, and he works on cool plugins like AffiliateWP, Easy Digital Downloads, and Restrict Content Pro.

13 thoughts on “Get email alerts about 404 errors on your WordPress site”

  1. hi!

    i think this is a very usefull addition for my site.
    so i included the script, all went fine and it works pretty best!

    the only little problem ist, my odd emailer (entourage/mac) can’t display the sent alert-email in the right way. first there is a long part of code, (for html-like presentation?), after it there shows up only the source of the table. do you possibly have one idea, how to get this all well displayed in any email-readers?

    thank you very much!
    cheers, Voxs

    Mod note: Code redacted

    1. It’s possible your email client doesn’t support HTML emails. On the other hand, you may need to adjust the HTML output in setup_email() to fit your own needs.

  2. Pardon my ignorance but…
    How do I exactly ‘require’ it in the functions.php file?
    I’ve googled a lot and cannot find the right way.
    By contrast, curiously people strongly advise against declaring classes inside theme files.

    1. Something like:

      require_once( get_template_directory() . 'class-404-emails.php' );

      There’s nothing wrong with declaring classes or class instances inside theme files. Personally, I like to break classes out to their own files then require them in functions as I noted in the post above.

  3. What about a hook to put everything the functions.php file? Like the template redirect hook?
    wordpress.stackexchange [com]/questions/1876/are-there-any-hooks-that-alter-the-404-logic

    1. Not sure I’m following. This class is a way to log 404 messages and have them sent to your email. We’re not circumventing the 404 query, we’re hooking into it to snag the errors.

Leave a Reply

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