WordPress Code Snippets

A fresh mix of code snippets and tutorials

WordPress 404 email alerts

116

Here is a customized plug-n-play PHP script for reporting 404 errors via email. No editing required, just clean email alerts for each 404 error.

Part of working online is cleaning up 404 errors for good SEO. Instead of taking the time to hunt them down manually or installing and maintaining another plugin, I add a small script to my theme’s 404.php file, so that I’m notified whenever 404 errors occur. This enables me to react swiftly to dead links, errors, and threats. Super-useful when working with many sites.

To implement this technique, simply include this script at the top of your theme’s 404.php file:

No editing required, just include and done. To disable the email reporting, either remove the script or comment-out the mail() function, like so:

// mail($email, "404 Alert: " . $blog . " [" . $theme . "]", $message, "From: $email");

Note that any of the variables (e.g., $time, $request, et al) are available for use elsewhere on the page. For example, you could display the information to the visitor to help deliver a useful 404 page.

Here is a screenshot showing how the email alerts will look in your inbox.

Update! Thanks to Drew Jaynes for sharing his OOP-enhanced version of the 404-email script. Muy bueno!!

Learn more

Digging into WordPress