• 客服微信1828868518 点击这里给我发消息
  • 下载资源,选择【普通下载】按钮即可

WordPress DUX 主题纯代码防止过滤拦截屏蔽垃圾评论

图文馆 1itao 3个月前 (08-31) 237次浏览 0个评论 扫描二维码

在实际情况中,我们网站的垃圾评论一般会有两个特点:一是大多数垃圾评论为外文,其二便是垃圾评论一般带有链接。那么针对这两个特点,把以下代码放在当前主题function.php文件中, 基本上可以过滤掉99%的垃圾评论。

<code><span class="com">// WordPress禁止全英文,不包括汉字的评论</span>
<span class="kwd">function</span><span class="pln"> refused_spam_comments</span><span class="pun">(</span><span class="pln"> $comment_data </span><span class="pun">)</span> <span class="pun">{</span><span class="pln"> 
$pattern </span><span class="pun">=</span> <span class="str">'/[一-龥]/u'</span><span class="pun">;</span> 
<span class="kwd">if</span><span class="pun">(!</span><span class="pln">preg_match</span><span class="pun">(</span><span class="pln">$pattern</span><span class="pun">,</span><span class="pln">$comment_data</span><span class="pun">[</span><span class="str">'comment_content'</span><span class="pun">]))</span> <span class="pun">{</span><span class="pln"> 
err</span><span class="pun">(</span><span class="str">'评论中必须含中文!'</span><span class="pun">);</span> 
<span class="pun">}</span> 
<span class="kwd">return</span><span class="pun">(</span><span class="pln"> $comment_data </span><span class="pun">);</span> 
<span class="pun">}</span><span class="pln"> 
add_filter</span><span class="pun">(</span><span class="str">'preprocess_comment'</span><span class="pun">,</span><span class="str">'refused_spam_comments'</span><span class="pun">);</span>

<span class="com">// WordPress禁止日文评论</span></code><code>
<span class="kwd">function</span><span class="pln"> BYMT_comment_jp_post</span><span class="pun">(</span><span class="pln"> $incoming_comment </span><span class="pun">)</span> <span class="pun">{</span><span class="pln">
$jpattern </span><span class="pun">=</span><span class="str">'/[ぁ-ん]+|[ァ-ヴ]+/u'</span><span class="pun">;</span>
<span class="kwd">if</span><span class="pun">(</span><span class="pln">preg_match</span><span class="pun">(</span><span class="pln">$jpattern</span><span class="pun">,</span><span class="pln"> $incoming_comment</span><span class="pun">[</span><span class="str">'comment_content'</span><span class="pun">])){</span><span class="pln">
err</span><span class="pun">(</span> <span class="str">"评论中禁止发日文字符!"</span> <span class="pun">);</span>
<span class="pun">}</span>
<span class="kwd">return</span><span class="pun">(</span><span class="pln"> $incoming_comment </span><span class="pun">);</span>
<span class="pun">}</span><span class="pln">
add_filter</span><span class="pun">(</span><span class="str">'preprocess_comment'</span><span class="pun">,</span> <span class="str">'BYMT_comment_jp_post'</span><span class="pun">);</span>

<span class="com">//Wordpress评论禁止发链接地址</span>
<span class="kwd">function</span><span class="pln"> lianyue_comment_post</span><span class="pun">(</span><span class="pln"> $incoming_comment </span><span class="pun">)</span> <span class="pun">{</span><span class="pln"> 
$http </span><span class="pun">=</span> <span class="str">'/[href="|rel="nofollow"|http:\/\/|<\/a>]/u'</span><span class="pun">;</span> 
<span class="kwd">if</span><span class="pun">(</span><span class="pln">preg_match</span><span class="pun">(</span><span class="pln">$http</span><span class="pun">,</span><span class="pln"> $incoming_comment</span><span class="pun">[</span><span class="str">'comment_content'</span><span class="pun">]))</span> <span class="pun">{</span><span class="pln"> 
err</span><span class="pun">(</span> <span class="str">"评论中禁止发链接地址!"</span> <span class="pun">);</span> 
<span class="pun">}</span> 
<span class="kwd">return</span><span class="pun">(</span><span class="pln"> $incoming_comment </span><span class="pun">);</span> 
<span class="pun">}</span><span class="pln"> 
add_filter</span><span class="pun">(</span><span class="str">'preprocess_comment'</span><span class="pun">,</span> <span class="str">'lianyue_comment_post'</span><span class="pun">);</span> </code>

亲测在dux主题中有效,其他主题的网站请自行测试~

WordPress DUX 主题纯代码防止过滤拦截屏蔽垃圾评论


喜欢 (0)
[]
分享 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址