Foreword
WordPress sites are frequently targeted by spam comments, which are usually generated in English and contain no Chinese characters. To effectively filter out spam comments, you can add a few lines of code to your theme’s template functions file (functions.php) to only allow comments containing Chinese characters to be submitted. This is a simple procedure that does not require installing any additional plugins.
Steps to Follow
- Open the
Theme File Editoras shown in the image below, click onTemplate Functions, paste the code into the file, and finally clickUpdate File.
// Only allow comments containing Chinese characters to be published
function block_non_chinese_comments($commentdata) {
$comment_content = $commentdata['comment_content'];
// Match Chinese characters using Unicode properties
if (!preg_match("/\\p{Han}/u", $comment_content)) {
// Terminate submission and display an error message if no Chinese characters are present
wp_die('<p style=\"text-align:center;font-size:18px;margin-top:50px;\">Error: Comment content is illegal!</p>',
'Error:',
array('response' => 403)
);
}
// Allow comment submission if Chinese characters are present
return $commentdata;
}
add_filter('preprocess_comment', 'block_non_chinese_comments');
- Result after submitting a non-Chinese comment.
Original link: https://wp.bufanz.com/wordpress/only-comments-containing-chinese-are-permitted.html


