为了杜绝垃圾评论,就添加了个验证码,效果非常不错,垃圾评论再也没有来了,方法非常简单,就是在function.php文件里添加个随机码的函数,在comments.php里相应的位置调用即可实现随机验证码,具体代码如下:
// ADD: Anti-spam Code
function hjyl_antispam(){
if(!is_user_logged_in()){
//$pcodes = substr(md5(mt_rand(11111,99999)),0,4);//English+Number
$pcodes = substr(mt_rand(0,99999),0,4); //Number
$str = '<p id="hjyl_anti">';
$str .= '<label for="subpcodes">'.__('Anti-spam Code').':</label>';
$str .= '<input type="text" size="4" id="subpcodes" name="subpcodes" maxlength="4" />';
$str .= '<span id="pcodes">'.$pcodes.'</span>';
$str .= '<input type="hidden" value="'.$pcodes.'" name="pcodes" />';
$str .= '</p>';
echo $str;
}
}
function yanzhengma(){
if ( !is_user_logged_in() ) {
$pcodes = trim($_POST['pcodes']);
$subpcodes = trim($_POST['subpcodes']);
if((($pcodes)!=$subpcodes) || empty($subpcodes)){
hjyl_comment_err( __('Error: Incorrect Anti-spam Code!') );
}
}
}
add_filter('pre_comment_on_post', 'yanzhengma');
add_action('comment_form', 'hjyl_antispam', 1, 1);
// ADD: for error
function hjyl_comment_err($a) {
header('HTTP/1.0 500 Internal Server Error');
header('Content-Type: text/plain;charset=UTF-8');
echo $a;
exit;
}
直接扔进functions.php里就可以了;
这也是我的插件hjyl-comment-spam验证码插件的代码版。
声明:本文采用 BY-NC-SA 协议进行授权,如无注明均为原创,转载请注明转自 你好!刘
本文地址:为WordPress添加评论验证码 – 代码版
本文地址:为WordPress添加评论验证码 – 代码版
发表评论