title: WordPress拒绝特定字符的垃圾评论
date: 2014-10-03 09:54:10
category: [编程代码]
tags: [Wordpress]
author: 零贰
<p> </p> <p>将以下代码粘贴到你的主题functions.php模板中。 <p>任何包含在$ bad_comment_content数组内的字符,将会被自动拒绝留言。 <p> </p><!--more-->
<p> </p> <p>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'])) { <br> $post_comment_content = $_POST['comment']; <br> $lower_case_comment = strtolower($_POST['comment']); <br> $bad_comment_content = array(
'充气','拉珠','G点','锁精环','t裤','夜总会','性用品','http:','字裤','夜总会','レ','メ','る',</p> <p> );
if (in_comment_post_like($lower_case_comment, $bad_comment_content)) {
$comment_box_text = wordwrap(trim($post_comment_content), 80, "n ", true);
$txtdrop = fopen('/var/log/httpd/wp_post-logger/nullamatix.com-text-area_dropped.txt', 'a'); <br> fwrite($txtdrop, " --------------n [COMMENT] = " . $post_comment_content . "\n --------------\n"); <br> fwrite($txtdrop, " [SOURCE_IP] = " . $_SERVER['REMOTE_ADDR'] . " @ " . date("F j, Y, g:i a") . "\n"); <br> fwrite($txtdrop, " [USERAGENT] = " . $_SERVER['HTTP_USER_AGENT'] . "\n"); <br> fwrite($txtdrop, " [REFERER ] = " . $_SERVER['HTTP_REFERER'] . "\n"); <br> 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( __('bang bang.') );
}
}
}
add_action('init', 'drop_bad_comments'); </p>