转载: WordPress非插件实现评论验证教程 - 思念博客

在当前主题的 functions.php 中添加以代码:

//评论添加验证码
function spam_protection_math(){
    $num1=rand(0,9);
    $num2=rand(0,9);
    echo " $num1 + $num2 = <input type='text' name='sum' class='math_textfield' value='' size='2' tabindex='4'>"
        ."<input type='hidden' name='num1' value='$num1'>"
        ."<input type='hidden' name='num2' value='$num2'>"
        ."<label for='math' class='small'> 请输入评论验证码</label>";
}
function spam_protection_pre($commentdata){
    $sum=$_POST['sum'];
    switch($sum){
        case $_POST['num1']+$_POST['num2']:break;
        case null:wp_die('对不起: 请输入验证码.');break;
        default:wp_die('对不起: 验证码错误,请重试.');
    }
    return $commentdata;
}

if($comment_data['comment_type']==''){
    add_filter('preprocess_comment','spam_protection_pre');
}

然后修改当前主题 comments.php 文件,找到输入发表评论信息的那些文字,代码如下:

< ?php spam_protection_math();?>
Last modification:October 9th, 2018 at 09:31 am