WordPress为了防止垃圾评论,有很多种方法,今天就说一个非常简单的方法。功能主要是:游客评论必须点击一下机器人验证,通过后才可以发表评论,而登录用户则不用点击,可直接发表评论。
第一步:
在functions.php文件中加入以下代码:
// 机器人评论验证
function siren_robot_comment(){
if ( !$_POST['no-robot'] && !is_user_logged_in()) {
err(__('请勾选机器人验证后在发表评论!Please comfirm you are not a robot.'));
}
}
add_action('pre_comment_on_post', 'siren_robot_comment');
第二步:
添加下列JS代码到你主题的js文件中
//机器人验证
$('.roboter').on('click',function(){
$('.robot-check-text').html('验证成功!');
});
第三步:
在comments.php文件的适当位置添加下列代码:
<label class="demo--label roboter" title="未登录用户需开启该功能后才可评论!"><input class="demo--radio robot-check" type="radio" name="no-robot"><span class="demo--radioInput"></span><span class="robot-check-text" >机器人验证</span></label>
第四步:
最后一把就是样式了,相信大家应该知道怎么获取吧