鸟哥曾经贴过一篇教程:《WordPress自动拒绝垃圾评论》,将以下代码粘贴到你的主题functions.php模板中即可实现拒绝评论中出现相关的关键字,代码如下:
- function in_comment_post_like($string, $array) {
- foreach($array as $ref) { if(strstr($string, $ref)) { return true; } }
- return false;
- }
- function drop_bad_comments() {
- if (!emptyempty($_POST['comment'])) {
- $post_comment_content = $_POST['comment'];
- $lower_case_comment = strtolower($_POST['comment']);
- $bad_comment_content = array(
- 'viagra',
- 'hydrocodone',
- 'hair loss',
- 'xanax',
- 'tramadol',
- 'russian girls',
- 'russian brides',
- 'lorazepam',
- 'adderall',
- 'dexadrine',
- 'no prescription',
- 'oxycontin',
- 'without a prescription',
- 'sex pics',
- 'family incest',
- 'online casinos',
- 'online dating',
- 'cialis',
- 'best forex',
- 'amoxicillin'
- );
- 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');
- 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');
第10到29行是关键字,只要按照上面的格式加入,如果评论包含关键字的话,就会提示错误。以后如果遇到新到垃圾评论关键字,在WordPress的后台外观-编辑-functions.php添加就可以了。
但是如果主题使用的是鸟哥的HotNewsPro的话,添加这段代码会提示出错,导致博客无法打开,原因是出在第43行,绝大多数的博客都可以使用wp_die,这个函数是返回给前台的用户看的,比如上面的代码,如果评论中包含关键字,就会返回:万恶的发贴机!,但是鸟哥的主题使用了自定义的函数,所以如果你也用的是鸟哥的主题,那么将wp_die改为err即可,就不会出现错误了