You are viewing an old revision of this post, from August 15, 2013 @ 03:16:10. See below for differences between this version and the current revision.

WordPress 404 email alerts

As seen on WP Mix: http://wp-mix.com/wordpress-404-email-alerts/ 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. To implement, simply include this script at the top of your theme’s 404.php file: [php] // WP 404 ALERTS @ http://wp-mix.com/wordpress-404-email-alerts/ // set status header("HTTP/1.1 404 Not Found"); header("Status: 404 Not Found"); // site info $blog = get_bloginfo('name'); $site = get_bloginfo('url') . '/'; $email = get_bloginfo('admin_email'); // theme info if (!empty($_COOKIE["nkthemeswitch" . COOKIEHASH])) { $theme = clean($_COOKIE["nkthemeswitch" . COOKIEHASH]); } else { $theme_data = wp_get_theme(); $theme = clean($theme_data->Name); } // referrer if (isset($_SERVER['HTTP_REFERER'])) { $referer = clean($_SERVER['HTTP_REFERER']); } else { $referer = "undefined"; } // request URI if (isset($_SERVER['REQUEST_URI']) && isset($_SERVER["HTTP_HOST"])) { $request = clean('http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]); } else { $request = "undefined"; } // query string if (isset($_SERVER['QUERY_STRING'])) { $string = clean($_SERVER['QUERY_STRING']); } else { $string = "undefined"; } // IP address if (isset($_SERVER['REMOTE_ADDR'])) { $address = clean($_SERVER['REMOTE_ADDR']); } else { $address = "undefined"; } // user agent if (isset($_SERVER['HTTP_USER_AGENT'])) { $agent = clean($_SERVER['HTTP_USER_AGENT']); } else { $agent = "undefined"; } // identity if (isset($_SERVER['REMOTE_IDENT'])) { $remote = clean($_SERVER['REMOTE_IDENT']); } else { $remote = "undefined"; } // log time $time = clean(date("F jS Y, h:ia", time())); // sanitize function clean($string) { $string = rtrim($string); $string = ltrim($string); $string = htmlentities($string, ENT_QUOTES); $string = str_replace("n", "<br>", $string); if (get_magic_quotes_gpc()) { $string = stripslashes($string); } return $string; } $message = "TIME: " . $time . "n" . "*404: " . $request . "n" . "SITE: " . $site . "n" . "THEME: " . $theme . "n" . "REFERRER: " . $referer . "n" . "QUERY STRING: " . $string . "n" . "REMOTE ADDRESS: " . $address . "n" . "REMOTE IDENTITY: " . $remote . "n" . "USER AGENT: " . $agent . "nnn"; mail($email, "404 Alert: " . $blog . " [" . $theme . "]", $message, "From: $email"); [/php] No editing required, just include and done. To disable the email reporting, either remove the script or comment-out the mail() function, like so: [php]// mail($email, "404 Alert: " . $blog . " [" . $theme . "]", $message, "From: $email");[/php] 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!!

Revisions

Revision Differences

August 15, 2013 @ 03:16:10Current Revision
Content
Unchanged: As seen on WP Mix: <a href="http:// wp-mix.com/wordpress-404- email-alerts/ ">http://wp- mix.com/wordpress- 404-email-alerts/</a>Unchanged: As seen on WP Mix: <a href="http:// wp-mix.com/wordpress-404- email-alerts/ ">http://wp- mix.com/wordpress- 404-email-alerts/</a>
Unchanged: Here is a customized plug-n-play <abbr title="PHP: Hypertext Preprocessor" >PHP</abbr> script for reporting 404 errors via email. No editing required, just clean email alerts for each 404 error.Unchanged: Here is a customized plug-n-play <abbr title="PHP: Hypertext Preprocessor" >PHP</abbr> script for reporting 404 errors via email. No editing required, just clean email alerts for each 404 error.
Unchanged: To implement, simply include this script at the top of your theme’s <code> 404.php</code> file:Unchanged: To implement, simply include this script at the top of your theme’s <code> 404.php</code> file:
Unchanged: [php]Unchanged: [php]
Unchanged: // WP 404 ALERTS @ http://wp-mix.com/ wordpress-404- email-alerts/Unchanged: // WP 404 ALERTS @ http://wp-mix.com/ wordpress-404- email-alerts/
Unchanged: // set statusUnchanged: // set status
Unchanged: header(&quot;HTTP/1.1 404 Not Found&quot;);Unchanged: header(&quot;HTTP/1.1 404 Not Found&quot;);
Unchanged: header(&quot;Status: 404 Not Found&quot;);Unchanged: header(&quot;Status: 404 Not Found&quot;);
Unchanged: // site infoUnchanged: // site info
Unchanged: $blog = get_bloginfo('name');Unchanged: $blog = get_bloginfo('name');
Unchanged: $site = get_bloginfo('url') . '/';Unchanged: $site = get_bloginfo('url') . '/';
Unchanged: $email = get_bloginfo( 'admin_email');Unchanged: $email = get_bloginfo( 'admin_email');
Unchanged: // theme infoUnchanged: // theme info
Unchanged: if (!empty($_COOKIE[&quot; nkthemeswitch&quot; . COOKIEHASH])) {Unchanged: if (!empty($_COOKIE[&quot; nkthemeswitch&quot; . COOKIEHASH])) {
Unchanged: $theme = clean($_COOKIE[&quot; nkthemeswitch&quot; . COOKIEHASH]);Unchanged: $theme = clean($_COOKIE[&quot; nkthemeswitch&quot; . COOKIEHASH]);
Unchanged: } else {Unchanged: } else {
Unchanged: $theme_data = wp_get_theme();Unchanged: $theme_data = wp_get_theme();
Unchanged: $theme = clean($theme_ data-&gt;Name);Unchanged: $theme = clean($theme_ data-&gt;Name);
Unchanged: }Unchanged: }
Unchanged: // referrerUnchanged: // referrer
Unchanged: if (isset($_SERVER['HTTP_REFERER'])) {Unchanged: if (isset($_SERVER['HTTP_REFERER'])) {
Unchanged: $referer = clean($_SERVER['HTTP_REFERER']);Unchanged: $referer = clean($_SERVER['HTTP_REFERER']);
Unchanged: } else {Unchanged: } else {
Unchanged: $referer = &quot;undefined&quot;;Unchanged: $referer = &quot;undefined&quot;;
Unchanged: }Unchanged: }
Unchanged: // request URIUnchanged: // request URI
Unchanged: if (isset($_SERVER['REQUEST_URI']) &amp;&amp; isset($_SERVER[&quot; HTTP_HOST&quot;])) {Unchanged: if (isset($_SERVER['REQUEST_URI']) &amp;&amp; isset($_SERVER[&quot; HTTP_HOST&quot;])) {
Unchanged: $request = clean('http://' . $_SERVER[&quot; HTTP_HOST&quot;] . $_SERVER[&quot; REQUEST_URI&quot;]);Unchanged: $request = clean('http://' . $_SERVER[&quot; HTTP_HOST&quot;] . $_SERVER[&quot; REQUEST_URI&quot;]);
Unchanged: } else {Unchanged: } else {
Unchanged: $request = &quot;undefined&quot;;Unchanged: $request = &quot;undefined&quot;;
Unchanged: }Unchanged: }
Unchanged: // query stringUnchanged: // query string
Unchanged: if (isset($_SERVER['QUERY_STRING'])) {Unchanged: if (isset($_SERVER['QUERY_STRING'])) {
Unchanged: $string = clean($_SERVER['QUERY_STRING']);Unchanged: $string = clean($_SERVER['QUERY_STRING']);
Unchanged: } else {Unchanged: } else {
Unchanged: $string = &quot;undefined&quot;;Unchanged: $string = &quot;undefined&quot;;
Unchanged: }Unchanged: }
Unchanged: // IP addressUnchanged: // IP address
Unchanged: if (isset($_SERVER['REMOTE_ADDR'])) {Unchanged: if (isset($_SERVER['REMOTE_ADDR'])) {
Unchanged: $address = clean($_SERVER['REMOTE_ADDR']);Unchanged: $address = clean($_SERVER['REMOTE_ADDR']);
Unchanged: } else {Unchanged: } else {
Unchanged: $address = &quot;undefined&quot;;Unchanged: $address = &quot;undefined&quot;;
Unchanged: }Unchanged: }
Unchanged: // user agentUnchanged: // user agent
Unchanged: if (isset($_SERVER['HTTP_ USER_AGENT'])) {Unchanged: if (isset($_SERVER['HTTP_ USER_AGENT'])) {
Unchanged: $agent = clean($_SERVER['HTTP_ USER_AGENT']);Unchanged: $agent = clean($_SERVER['HTTP_ USER_AGENT']);
Unchanged: } else {Unchanged: } else {
Unchanged: $agent = &quot;undefined&quot;;Unchanged: $agent = &quot;undefined&quot;;
Unchanged: }Unchanged: }
Unchanged: // identityUnchanged: // identity
Unchanged: if (isset($_SERVER['REMOTE_IDENT'])) {Unchanged: if (isset($_SERVER['REMOTE_IDENT'])) {
Unchanged: $remote = clean($_SERVER['REMOTE_IDENT']);Unchanged: $remote = clean($_SERVER['REMOTE_IDENT']);
Unchanged: } else {Unchanged: } else {
Unchanged: $remote = &quot;undefined&quot;;Unchanged: $remote = &quot;undefined&quot;;
Unchanged: }Unchanged: }
Unchanged: // log timeUnchanged: // log time
Unchanged: $time = clean(date(&quot;F jS Y, h:ia&quot;, time()));Unchanged: $time = clean(date(&quot;F jS Y, h:ia&quot;, time()));
Unchanged: // sanitizeUnchanged: // sanitize
Unchanged: function clean($string) {Unchanged: function clean($string) {
Unchanged: $string = rtrim($string);Unchanged: $string = rtrim($string);
Unchanged: $string = ltrim($string);Unchanged: $string = ltrim($string);
Unchanged: $string = htmlentities($string, ENT_QUOTES);Unchanged: $string = htmlentities($string, ENT_QUOTES);
Deleted: $string = str_replace(&quot;\n&quot;, &quot;&lt;br&gt;&quot;, $string); Added: $string = str_replace(&quot;n&quot;, &quot;&lt;br&gt;&quot;, $string);
Unchanged: if (get_magic_quotes_gpc()) {Unchanged: if (get_magic_quotes_gpc()) {
Unchanged: $string = stripslashes($string);Unchanged: $string = stripslashes($string);
Unchanged: }Unchanged: }
Unchanged: return $string;Unchanged: return $string;
Unchanged: }Unchanged: }
Unchanged: $message =Unchanged: $message =
Deleted: &quot;TIME: &quot; . $time . &quot;\n&quot; . Added: &quot;TIME: &quot; . $time . &quot;n&quot; .
Deleted: &quot;*404: &quot; . $request . &quot;\n&quot; . Added: &quot;*404: &quot; . $request . &quot;n&quot; .
Deleted: &quot;SITE: &quot; . $site . &quot;\n&quot; . Added: &quot;SITE: &quot; . $site . &quot;n&quot; .
Deleted: &quot;THEME: &quot; . $theme . &quot;\n&quot; . Added: &quot;THEME: &quot; . $theme . &quot;n&quot; .
Deleted: &quot;REFERRER: &quot; . $referer . &quot;\n&quot; . Added: &quot;REFERRER: &quot; . $referer . &quot;n&quot; .
Deleted: &quot;QUERY STRING: &quot; . $string . &quot;\n&quot; . Added: &quot;QUERY STRING: &quot; . $string . &quot;n&quot; .
Deleted: &quot;REMOTE ADDRESS: &quot; . $address . &quot;\n&quot; . Added: &quot;REMOTE ADDRESS: &quot; . $address . &quot;n&quot; .
Deleted: &quot;REMOTE IDENTITY: &quot; . $remote . &quot;\n&quot; . Added: &quot;REMOTE IDENTITY: &quot; . $remote . &quot;n&quot; .
Deleted: &quot;USER AGENT: &quot; . $agent . &quot;\n\n\n&quot;; Added: &quot;USER AGENT: &quot; . $agent . &quot;nnn&quot;;
Unchanged: mail($email, &quot;404 Alert: &quot; . $blog . &quot; [&quot; . $theme . &quot;]&quot;, $message, &quot;From: $email&quot;);Unchanged: mail($email, &quot;404 Alert: &quot; . $blog . &quot; [&quot; . $theme . &quot;]&quot;, $message, &quot;From: $email&quot;);
Unchanged: [/php]Unchanged: [/php]
Unchanged: No editing required, just include and done. To disable the email reporting, either remove the script or comment-out the <code>mail( )</code> function, like so:Unchanged: No editing required, just include and done. To disable the email reporting, either remove the script or comment-out the <code>mail( )</code> function, like so:
Unchanged: [php]// mail($email, &quot;404 Alert: &quot; . $blog . &quot; [&quot; . $theme . &quot;]&quot;, $message, &quot;From: $email&quot;);[/php]Unchanged: [php]// mail($email, &quot;404 Alert: &quot; . $blog . &quot; [&quot; . $theme . &quot;]&quot;, $message, &quot;From: $email&quot;);[/php]
Unchanged: Note that any of the variables (e.g., <code> $time</code>, < code>$request</code>, 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.Unchanged: Note that any of the variables (e.g., <code> $time</code>, < code>$request</code>, 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.
Deleted: <a href="http:// wp-mix.com/wp/wp-content/ uploads/email- 404-alert.gif">Here is a screenshot</a> showing how the email alerts will look in your inbox. Added: <a href="http:// wiki.pixelpress.com.au/files/ 2013/08/email- 404-alert.gif">Here is a screenshot</a> showing how the email alerts will look in your inbox.
Unchanged: Update! Thanks to <a href="https:/ /twitter.com/ DrewAPicture/ status/306120533202837505">Drew Jaynes</a> for sharing his <a href="https:/ /gist.github.com/ DrewAPicture/ 5032207">OOP-enhanced version of the 404-email script</a>. Muy bueno!!Unchanged: Update! Thanks to <a href="https:/ /twitter.com/ DrewAPicture/ status/306120533202837505">Drew Jaynes</a> for sharing his <a href="https:/ /gist.github.com/ DrewAPicture/ 5032207">OOP-enhanced version of the 404-email script</a>. Muy bueno!!

Note: Spaces may be added to comparison text to allow better line wrapping.

Tags:

No comments yet.

Leave a Reply