代码加上了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();
?>