如下,有問題追問或本人網站留言。
有時候我們的博客需要分享壹些羞羞的內容,按慣例及服務器所在國家法律要求,是必須要在瀏覽前顯示“成人內容警告”的,以提示方可內容可能不適宜未成年人或引起不適。對於wordpress來說就很簡單,簡單的壹段代碼就能實現。
要實現這樣的功能,我們需要wordpress在wp_loaded的時候判斷指定cookie是否存在,以確定是否是該瀏覽者是否已經瀏覽警告頁面。如果該cookie不存在則顯示警告頁面,並且在瀏覽者點擊確認後寫入cookie。
代碼如下:
if(class_exists('AdultContentsWarning'))?return;class?AdultContentsWarning{
var?$path;
function?__construct(){
add_action('wp_loaded',array($this,'isAdult'));
$path=?parse_url(untrailingslashit(home_url()))['path']?===?NULLparse_url($_SERVER['REQUEST_URI']?)['path']?:
explode(parse_url(untrailingslashit(home_url()))['path'],parse_url($_SERVER['REQUEST_URI']?)['path'])[0];
}
function?isAdult(){
if(is_user_logged_in()?===?true)?return;
if(!isset($_COOKIE['isAdult'])?||?$_COOKIE['isAdult']?!=?'yes'){
if($_POST['isAdult']?==?'yes'){
if(setcookie('isAdult','yes',time()+3600*24)?===?true){
echo?'success';
die;
}
}
else{
$this->show_warning();
die();
}
}
return;
}
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(){
$.post("",{isAdult:'yes'},function(result){
if(result?==?'success'){
if?(getCookie('isAdult')?!=?false){
location.reload(true);
}else{
alert('Cookie寫入失敗!');
location.reload(false);
}
}
});
});
$("#close").click(function(){
if(window.history.go(-1)?==?undefined){
window.location.href="about:blank";
}
else{
window.history.go(-1);
}
});
});
function?getCookie(c_name)
{
if?(document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name?+?"=")
if?(c_start!=-1)
{
c_start=c_start?+?c_name.length+1
c_end=document.cookie.indexOf(";",c_start)
if?(c_end==-1)?c_end=document.cookie.length
return?unescape(document.cookie.substring(c_start,c_end))
}
}
return?false
}
</script>
</body></html>
<?php
}
}
new?AdultContentsWarning();