Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Original snippet by wp-mix.com at http://wp-mix.com/wordpress-404-email-alerts/ With the improved OOP style, implementation is a lot more straightforward, just add <?php new Clean_404_Email; to the top of your 404.php template.

View clean_404_emails.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
<?php
// Just require() this class via your theme's functions.php file.
/* Then instantiate this at the top of your 404.php file with:
if ( class_exists( 'Clean_404_Email' ) )
new Clean_404_Email;
*/
class Clean_404_Email {
var $time, $request, $blog, $email, $theme, $theme_data, $site, $referer, $string, $address, $remote, $agent, $message;
function __construct() {
$this->headers();
$this->setup_vars();
$this->setup_email();
$this->send_mail();
}
function headers() {
echo header( "HTTP/1.1 404 Not Found" );
echo header( "Status: 404 Not Found" );
}
function setup_vars() {
$this->blog = get_bloginfo( 'name' );
$this->site = home_url( '/' );
$this->email = get_bloginfo( 'admin_email' );
// theme info
if ( ! empty( $_COOKIE["nkthemeswitch" . COOKIEHASH] ) ) {
$this->theme = $this->clean( $_COOKIE["nkthemeswitch" . COOKIEHASH] );
} else {
$this->theme_data = wp_get_theme();
$this->theme = $this->clean( $this->theme_data->Name );
}
// referrer
$this->referer = isset( $_SERVER['HTTP_REFERER'] ) ? $this->clean( $_SERVER['HTTP_REFERER'] ) : 'undefined';
// request URI
$this->request = isset( $_SERVER['REQUEST_URI'] ) && isset( $_SERVER['HTTP_HOST'] ) ? $this->clean( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ) : 'undefined';
// query string
$this->string = isset( $_SERVER['QUERY_STRING'] ) ? $this->clean( $_SERVER['QUERY_STRING'] ) : 'undefined';
// IP address
$this->address = isset( $_SERVER['REMOTE_ADDR'] ) ? $this->clean( $_SERVER['REMOTE_ADDR'] ) : 'undefined';
// user agent
$this->agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $this->clean( $_SERVER['HTTP_USER_AGENT'] ) : 'undefined';
// identity
$this->remote = isset( $_SERVER['REMOTE_IDENT'] ) ? $this->clean( $_SERVER['REMOTE_IDENT'] ) : 'undefined';
// log time
$this->time = $this->clean( date( "F jS Y, h:ia", time() ) );
}
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;
}
function setup_email() {
$this->message = '
<html><body>
<table width="70%" border="1" style="border-color: #777;" cellpadding="10">
<colgroup width="25%" style="text-align:right;" />
<colgroup id="colgroup" class="colgroup" width="1*" valign="middle" span="2" align="center" />
<thead>
<tr>
<th scope="col">Element</th>
<th scope="col">Data</th>
</tr>
</thead>
<tfoot>
<tr>
<td>User Agent</td>
<td>' . $this->agent . '</td>
</tr>
</tfoot>
<tbody>
<tr>
<td>Date/Time</td>
<td>' . $this->time . '</td>
</tr>
<tr>
<td>404 URL</td>
<td>' . $this->request . '</td>
</tr>
<tr>
<td>Site</td>
<td>' . $this->site . '</td>
</tr>
<tr>
<td>Theme</td>
<td>' . $this->theme . '</td>
</tr>
<tr>
<td>Referer</td>
<td>' . $this->referer . '</td>
</tr>
<tr>
<td>Query String</td>
<td>' . $this->string . '</td>
</tr>
<tr>
<td>Remote Address</td>
<td>' . $this->address . '</td>
</tr>
<tr>
<td>Remote Identity</td>
<td>' . $this->remote . '</td>
</tr>
</tbody>
</table>
</body></html>';
}
function email_headers() {
return sprintf( 'From: %s' . "\r\n" . 'MIME-Version: 1.0' . "\r\n" . 'Content-Type: text/html; charset=ISO-8859-1' . "\r\n", $this->email );
}
function send_mail() {
mail( $this->email, sprintf( '404 Alert: %1$s [ %2$s ]', $this->blog, $this->theme ), $this->message, sprintf( 'From: %s', $this->email_headers() ) );
}
} // Clean_404_Email

Added a not-completely-awful HTML table for the email: http://cl.ly/image/0G2Q0N0K3y1k

Hi Drew,

i've pasted your code on my "404.php" file, placed on my child theme. But i'm not getting the email. I'm sure the 404 archive loaded is the one on my child folder, cause i've translated it to spanish. So my question is...
Do i have to change any code line to make it work? email or something? i'm the admin, so i didn't change anything at first, then i've changed 'admin_email' on line 27 with my actual email without results.

This article says i have to change some lines, even thought the code is not exactly the same.

Sorry i'm a beginner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.