為了傷害那些在評論區亂打一通的人,雖然在國內用的少吧,可以選擇性的加上去。
為了傷害那些在評論區亂打一通的人,雖然在國內用的少吧,可以選擇性的加上去。
在主題根目錄下的functions.php
文件中的<?php
下添加以下代碼并保存。
/*禁止純英文、純日文評論
* http://m.kartiktrivedi.com/18129.html
* */
function refused_english_comments($incoming_comment) {
$pattern = '/[一-龥]/u';
// 禁止全英文評論
if(!preg_match($pattern, $incoming_comment['comment_content'])) {
wp_die( "您的評論中必須包含漢字!" );
}
$pattern = '/[あ-んア-ン]/u';
// 禁止日文評論
if(preg_match($pattern, $incoming_comment['comment_content'])) {
wp_die( "評論禁止包含日文!" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'refused_english_comments');
實戰
- 優化正則
- 添加返回按鈕,避免用戶誤操作
public static function refused_english_comments($incoming_comment)
{
$pattern = '/[p{Script=Han}]/u';
if (!preg_match($pattern, $incoming_comment['comment_content'])) {
$message = '您的評論中必須包含漢字!';
$message .= '<br/><a href="#" onclick="history.back();">
<button class="button" style="margin: 1em 0;">返回</button>
</a>';
wp_die($message);
}
return $incoming_comment;
}