Wordpress主题技巧

WordPress站点出现404错误时邮件通知管理员

WordPress站点出现404错误时邮件通知管理员

几乎所有的网站都会出现404错误,也就是所访问的页面不存在。之前分享过几个 WordPress 404错误统计及重定向插件,今天主要分享下 WordPress站点出现404错误时自动发送邮件通知管理员的方法。

email-404-alert-wpdaxue_com

将下面的代码添加到你所用的WordPress主题的 404.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
<?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   . "\n\n\n";
 
wp_mail($email, "404 Alert: " . $blog . " [" . $theme . "]", $message, "From: $email"); 
 
?>

<?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 . "\n\n\n";
wp_mail($email, "404 Alert: " . $blog . " [" . $theme . "]", $message, "From: $email");
?>

如果你的主机支持mail()函数或者设置了SMTP发送邮件,总之就是能够发送邮件就行,一会去查看你设置的WordPress站点的管理员邮箱,你就会发现404通知邮件(可能在垃圾箱哦),邮件内容如本文配图所示。

如果你不需要该功能,删除刚才添加的代码,或者注销代码最后的mail()函数:

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

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

参考资料:http://wp-mix.com/wordpress-404-email-alerts/

Wordpress主题技巧

更多有关于 Wordpress主题技巧

塔克网整站模板 安装使用指南

WordPress如何备份网站数据库和文件?WordPress整站搬家操作流程。

WordPress网站安全设置教程

国外英文广告联盟网站模板,WordPress主题整站演示地址列表

WordPress主题开发:开启文章缩略图功能

WordPress 自定义头像插件:WP User Avatar

WordPress 后台管理界面UI自定义插件:WP Admin UI Customize

WordPress 自定义文章/页面阅读权限的插件:User Specific Content

10 个免费的 WordPress 备份插件

本人擅长Ai、Fw、Fl、Br、Ae、Pr、Id、Ps等软件的安装与卸载,精通CSS、JavaScript、PHP、ASP、C、C++、C#、Java、Ruby、Perl、Lisp、Python、Objective-C、ActionScript、Pascal等单词的拼写,熟悉Windows、Linux、OS X、Android、iOS、WP8等系统的开关机。

通过下面的方式来联系我们:

电邮:138762189@qq.com

联系QQ:点击这里给我发消息

官方站:www.tadke.com

※ ※ 联系请加我的企鹅号 ※※

※ ※技术支持请微信联系站长 ※※

Copyright © 2016 Tadke.com. Powered by Wordpress.