有网友反馈说wordpress成人内容警告代码 中的代码存在问题,在他的网站中使用发生错误。
抽空看了一下,发现之前写的太过潦草,没考虑兼容性的问题,并且设计过于复杂。
其实在这类功能中和服务器的交互越少越好,只要能在前端展现提醒即可,甚至提醒内容都可以通过js展示。
由于我这用的是php生成的提醒内容,所以今天又加上了一个user_agent判断,不对搜索引擎的抓取行为显示提醒,避免搜索引擎误判网页内容。
另外修改了逻辑问题,直接复制粘贴到主题functions.php即可使用。
如需修改样式,修改中间<html>内的html代码即可,本人不才,未进一步完善样式。
如哪位朋友需要个性化定制,可留言提出你的需求。
class AdultContentsWarning {
function __construct() {
if (isset($_COOKIE['isAdult']) && $_COOKIE['isAdult'] == 'yes') return;
if (stripos($_SERVER['PHP_SELF'],'/wp-admin/') !== false || stripos($_SERVER['PHP_SELF'],'/wp-login.php') !== false) return;
$spiders = array('baidu','sogou','spider','yodao');
foreach($spiders as $spiderName){
if(stripos($_SERVER['HTTP_USER_AGENT'],$spiderName) !== false) return;
}
add_action('wp_loaded', array($this, 'checkAdult'));
}
function checkAdult() {
if (is_user_logged_in() === true) return;
$this->show_warning();
die();
}
private function show_warning() {
?>
<html>
<head>
<title>成人内容警告 - <?php bloginfo('name'); ?></title>
<meta name="description" content="">
<meta name="keywords" content="">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script type="text/javascript" src="<?php echo home_url('/wp-includes/js/jquery/jquery.js'); ?>"></script>
</head>
<style type="text/css">
a{text-decoration:none;color:#0000FF}
a:hover{text-decoration:underline;}
</style>
<body bgcolor="#FFFFFF" text="#000000">
<p align="center"><font color="#FF0000"><b>警告 / WARNING </b><br>
</font><br>
本物品內容可能令人反感;不可將本物品內容派發,傳閱,出售,出租,交給<br>或出借予年齡未滿 18 歲的人士出示,播放或播映。<br>
<br>
<font size="1" face="Verdana">This article contains material which may offernd and may not be distributed, circulated, sold, hired, given, lent, shown, <br>
played or projected to a person under the age of 18 years. All models are 18 or older. </font><br>
</p>
<p align="center">
<b>
<font size="7" face="Verdana">
<a href="javascript::" id="confirm">__ 滿 18 歲,請按此 __</a></font></b><BR><BR>
<a href="javascript:" id="close">退出浏览</a>
</p>
<script>
jQuery(document).ready(function($) {
$("#confirm").click(function(){
var name = 'isAdult';
var value = 'yes';
var exp = new Date();
exp.setTime(exp.getTime() + 1*24*60*60*1000); //设置过期时间为一天
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
document.location.reload();
});
$("#close").click(function(){
if(window.history.go(-1) == undefined){
window.location.href="about:blank";
}
else{
window.history.go(-1);
}
});
});
</script>
</body></html>
<?php
}
}
new AdultContentsWarning();
?>
2018年12月5日 下午4:39 沙发
仰望大佬
2018年12月8日 下午7:59 1层
@吃馒头的猫 被大佬称大佬是一种绝妙的体验,哈哈哈
2019年3月5日 下午4:11 板凳
来打个卡
2019年4月2日 上午2:36 地板
请问这警告页面有插件可以做到吗?如果有可以说下名字吗?
因为我使用这个代码跟我另外一个插件冲突了。
2019年4月23日 下午9:24 1层
@熊疯 我没用过,这个是一个朋友让帮忙写的就随便写了下,功能比较简单。