Mail notifies admin when 404 error occurs in WordPress site

Source: Internet
Author: User

Add the following code to the top of the 404.php file on the WordPress theme you are using:

If your host supports the mail () function or if you set up SMTP to send mail, in short, it is able to send mail on the line, a will go to see you set up the WordPress site administrator mailbox, you will find 404 notification mail (may be in the garbage bin), the content of the message as shown in this article map.

The code is as follows Copy Code

Mail ($email, "404 Alert:". $blog. " [" . $theme. "]", $message, "from: $email");

Otherwise, we'll do it the following way.

The code is as follows Copy Code

<?php//WP 404 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");

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.