WordPress站点出现404错误时邮件通知管理员的方法
发布时间:2014-12-27 15:08:12 作者:佚名 我要评论
这篇文章主要为大家介绍了WordPress站点出现404错误时邮件通知管理员的方法,可实现出现404错误的情况下邮件通知管理员的功能,对网站优化很有帮助,需要的朋友可以参考下
本文实例讲述了WordPress站点出现404错误时邮件通知管理员的方法。分享给大家供大家参考。具体分析如下:
这是一个比较实用的功能,当我们网站出现了错误时就邮箱通知管理员,将下面的代码添加到你所用的WordPress主题的 404.php 文件的顶端即可.
如果你的主机支持mail()函数或者设置了SMTP发送邮件,总之就是能够发送邮件就行,一会去查看你设置的WordPress站点的管理员邮箱,你就会发现404通知邮件,可能在垃圾箱哦.
实例代码如下:
复制代码
代码如下:mail($email, "404 Alert: " . $blog . " [" . $theme . "]", $message, "From: $email");
完整代码如下:
复制代码
代码如下:<?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 (!emptyempty($_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", "
", $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");
?>
// 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 (!emptyempty($_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", "
", $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");
?>
希望本文所述对大家的WordPress建站有所帮助。
相关文章
- WordPress发送邮件的函数是wp_mail(),默认以纯文本(text/plain)格式发送,通过wp_mail()提供的filter函数可以更改为以HTML格式发送,也可以通过制定wp_mail()的第四个参2012-07-26
WordPress 有人回复时邮件通知的实现方法 SAE 移植版
常见的 Wordress 邮件通知功能,使用起来非常的简单2012-06-07- 本文详介如何给WordPress投稿功能添加邮件提醒功能2012-09-28
- 这篇文章主要为大家介绍了WordPress修改新用户注册邮件内容的方法,介绍了两种方法可实现针对注册邮箱内容的个性化修改,是比较实用的技巧,需要的朋友可以参考下2014-12-16
- 通常我们喜欢使用Wordpress Thread Comment 或 Mail To Commenter这两款插件前者久未更新,已测支持WordPress2.9.2版本 其他的未测试,优点,不进垃圾箱,支持嵌套回复,今2013-01-14
- 在浏览网页的时候, 我们经常会看到很多人在他们网站的搜索栏里显示文字提示, 当鼠标点击搜索栏, 则提示信息消失.2011-05-10
- WordPress在使用一切正常,无意查看源代码发现中文字符为乱码。潜意识告诉我们,应该是编码问题,问题应该是主题模板的变法,于是把所有模板文件都用EDITPLUS另存问UFT-8变2010-06-06
- Wordpress自带着wp_tag_cloud()函数,但只在页面侧边显示往往就不够了.需要一个单页来放所有的Tags2010-01-24
- 网上搜了一下“忘记wordpress的登录密码”, 可以找到一大堆的解决方法,比如通过邮件讨回,但这个需要服务器支持,还有通过直接访问数据库更改的,但也至少需要去安装个my2010-05-28
最新评论