首先在wp-content/themes/风格中的functions.php末尾,添加 functions-custom.php 文件
在functions.php的末尾添加一行:
require ‘functions-custom.php’;
思路1:评论中一定要包含中文
/* refused spam */
function refused_spam_comments( $comment_data ) {
$pattern = '/[一-龥]/u';
if(!preg_match($pattern,$comment_data['comment_content'])) {
wp_die('评论必须含中文!');
}
return( $comment_data );
}
add_filter('preprocess_comment','refused_spam_comments');
思路2:评论中包含禁止内容
function in_comment_post_like($string, $array) {
foreach($array as $ref) { if(strstr($string, $ref)) { return true; } }
return false;
}
function drop_bad_comments() {
if (!empty($_POST['comment'])) {
$post_comment_content = $_POST['comment'];
$lower_case_comment = strtolower($_POST['comment']);
$bad_comment_content = array(
'http',
'www.',
'XXX'
);
if (in_comment_post_like($lower_case_comment, $bad_comment_content)) {
$comment_box_text = wordwrap(trim($post_comment_content), 80, "\n ", true);
$txtdrop = fopen('/tmp/log.tangtang.org.antispam.txt', 'a');
fwrite($txtdrop, " --------------\n [COMMENT] = " . $post_comment_content . "\n --------------\n");
fwrite($txtdrop, " [SOURCE_IP] = " . $_SERVER['REMOTE_ADDR'] . " @ " . date("F j, Y, g:i a") . "\n");
fwrite($txtdrop, " [USERAGENT] = " . $_SERVER['HTTP_USER_AGENT'] . "\n");
fwrite($txtdrop, " [REFERER ] = " . $_SERVER['HTTP_REFERER'] . "\n");
fwrite($txtdrop, " [FILE_NAME] = " . $_SERVER['SCRIPT_NAME'] . " - [REQ_URI] = " . $_SERVER['REQUEST_URI'] . "\n");
fwrite($txtdrop, '--------------**********------------------'."\n");
header("HTTP/1.1 406 Not Acceptable");
header("Status: 406 Not Acceptable");
header("Connection: Close");
wp_die( __('评论中包含不合时宜的内容,请返回修改.') );
}
}
}
add_action('init', 'drop_bad_comments');
当然,把两个加上都可以。
以上代码来自wpdaxue
评论链接文件在:wp-includes/comment-template.php
修改为:
//$return = "<a href='$url' rel='external nofollow' class='url'>$author</a>";
$return = "<a href='#' rel='external nofollow' class='url' title='$url'>$author</a>";
这样也不妨碍我们了解评论人的网站。