昰道 發表於 2023-11-26 00:00:00

Wordpress自动将包含长链接的评论标记为垃圾评论的方法

<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
本文实例讲述了Wordpress自动将包含长链接的评论标记为垃圾评论的方法。分享给大家供大家参考。具体分析如下:</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
现在大量的机器可以直接对WordPress博客进行评论并带有大量的连接,这些带链接评论我们定为垃圾评论了,这里就来给大家介绍几个自动将包含长链接的评论标记为垃圾评论的方法.</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
方法一:此方法依旧是来自 Willin Kan 大师(可惜他已经退出WordPress圈),折腾很简单,直接将下面的代码放到主题的functions.php文件的最后一个 ?&gt;前面即可,代码如下:<br>
 </p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode3" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
// 垃圾评论拦截 <br>
class anti_spam { <br>
function anti_spam() { <br>
if ( !current_user_can('level_0') ) { <br>
add_action('template_redirect', array($this, 'w_tb'), 1); <br>
add_action('init', array($this, 'gate'), 1); <br>
add_action('preprocess_comment', array($this, 'sink'), 1); <br>
} <br>
} <br>
function w_tb() { <br>
if ( is_singular() ) { <br>
ob_start(create_function('$input','return preg_replace("#textarea(.*?)name=(["'])comment(["'])(.+)/textarea&gt;#", <br>
"textarea$1name=$2w$3$4/textarea&gt;&lt;textarea name="comment" cols="100%" rows="4" style="display:none"&gt;&lt;/textarea&gt;",$input);') ); <br>
} <br>
} <br>
function gate() { <br>
if ( !emptyempty($_POST['w']) &amp;&amp; emptyempty($_POST['comment']) ) { <br>
$_POST['comment'] = $_POST['w']; <br>
} else { <br>
$request = $_SERVER['REQUEST_URI']; <br>
$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '隐瞒'; <br>
$IP = isset($_SERVER["HTTP_X_FORWARDED_FOR"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] . ' (透过代理)' : $_SERVER["REMOTE_ADDR"]; <br>
$way = isset($_POST['w']) ? '手动操作' : '未经评论表格'; <br>
$spamcom = isset($_POST['comment']) ? $_POST['comment'] : null; <br>
$_POST['spam_confirmed'] = "请求: ". $request. "n来路: ". $referer. "nIP: ". $IP. "n方式: ". $way. "n?热? ". $spamcom. "n -- 记录成功 --"; <br>
} <br>
} <br>
function sink( $comment ) { <br>
if ( !emptyempty($_POST['spam_confirmed']) ) { <br>
if ( in_array( $comment['comment_type'], array('pingback', 'trackback') ) ) return $comment; <br>
//方法一: 直接挡掉, ? die(); 前面两斜线?h除即可. <br>
die(); <br>
//方法二: 标记为 spam, 留在资料库检查是否误判. <br>
//add_filter('pre_comment_approved', create_function('', 'return "spam";')); <br>
//$comment['comment_content'] = "[ 小墙判断这是 Spam! ]n". $_POST['spam_confirmed']; <br>
} <br>
return $comment; <br>
} <br>
} <br>
$anti_spam = new anti_spam();</div>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>这个方法可以阻止98%以上的垃圾评论,当然了,倡萌还建议你审核第一次提交的评论人的评论,设置审核后才显示,如果遇到垃圾评论,将其email、IP、网址等添加到黑名单即可,下面是倡萌目前的评论设置,你可以在WP后台-设置-讨论下设置.</span>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
 </p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
方法二:自动拒绝包含特定关键词的垃圾评论,将下面的代码添加到主题的functions.php文件,自己根据需要,修改 $ bad_comment_content 数组的内容,任何包含在$ bad_comment_content 数组内的字符,将会被自动拒绝留言,代码如下:<br>
 </p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode4" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
function in_comment_post_like($string, $array) { <br>
foreach($array as $ref) { if(strstr($string, $ref)) { return true; } } <br>
return false; <br>
} <br>
function drop_bad_comments() { <br>
if (!emptyempty($_POST['comment'])) { <br>
$post_comment_content = $_POST['comment']; <br>
$lower_case_comment = strtolower($_POST['comment']); <br>
$bad_comment_content = array( <br>
'viagra', <br>
'hydrocodone', <br>
'hair loss', <br>
'xanax', <br>
'tramadol', <br>
'russian girls', <br>
'russian brides', <br>
'lorazepam', <br>
'adderall', <br>
'dexadrine', <br>
'no prescription', <br>
'oxycontin', <br>
'without a prescription', <br>
'sex pics', <br>
'family incest', <br>
'online casinos', <br>
'online dating', <br>
'cialis', <br>
'best forex', <br>
'amoxicillin' <br>
); <br>
if (in_comment_post_like($lower_case_comment, $bad_comment_content)) { <br>
$comment_box_text = wordwrap(trim($post_comment_content), 80, "n ", true); <br>
$txtdrop = fopen('/var/log/httpd/wp_post-logger/nullamatix.com-text-area_dropped.txt', 'a'); <br>
fwrite($txtdrop, " --------------n = " . $post_comment_content . "n --------------n"); <br>
fwrite($txtdrop, " = " . $_SERVER['REMOTE_ADDR'] . " @ " . date("F j, Y, g:i a") . "n"); <br>
fwrite($txtdrop, " = " . $_SERVER['HTTP_USER_AGENT'] . "n"); <br>
fwrite($txtdrop, " = " . $_SERVER['HTTP_REFERER'] . "n"); <br>
fwrite($txtdrop, " = " . $_SERVER['SCRIPT_NAME'] . " - = " . $_SERVER['REQUEST_URI'] . "n"); <br>
fwrite($txtdrop, '--------------**********------------------'."n"); <br>
header("HTTP/1.1 406 Not Acceptable"); <br>
header("Status: 406 Not Acceptable"); <br>
header("Connection: Close"); <br>
wp_die( __('bang bang.') ); <br>
} <br>
} <br>
} <br>
add_action('init', 'drop_bad_comments');</div>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>今天再补充下,自动将包含长链接的评论标记为垃圾评论,将下面的代码添加到主题的 functions.php 文件即可:</span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode5" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
function rkv_url_spamcheck( $approved , $commentdata ) { <br>
return ( strlen( $commentdata['comment_author_url'] ) &gt; 50 ) ? 'spam' : $approved; <br>
} <br>
add_filter( 'pre_comment_approved', 'rkv_url_spamcheck', 99, 2 );</div>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>注意看第二行的 50,根据自己的需要修改这个数值,如果你希望将所有带有链接(不管是否是长链接)的评论内容都自动标记为垃圾评论,将 50 改为 1 即可.</span>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
 </p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
希望本文所述对大家的WordPress建站有所帮助。</p>
頁: [1]
查看完整版本: Wordpress自动将包含长链接的评论标记为垃圾评论的方法