Publish: 2014-11-22 | Modify: 2018-09-25
Blog has just been set up, and annoying spam comments are coming again. This is a headache for many beginners, and some even directly use spam comments to launch attacks and overload your database. Therefore, it is necessary for us to take some preventive measures.
From the analysis of spam comments, it can be roughly concluded that most of them are robot comments, characterized by all English or all Japanese information, which is very bad. Therefore, we should not allow such comments to appear, even writing them into the database is not allowed. Just add the following code to the functions.php file in your theme directory to solve it.
// Block pure English comments and pure Japanese comments
function inlojv_comment_post( $incoming_comment ) {
$pattern = '/[一-龥]/u';
// Block all English comments
if(!preg_match($pattern, $incoming_comment['comment_content'])) {
wp_die( "Your comment must contain Chinese characters!" );
}
$pattern = '/[あ-んア-ン]/u';
// Block Japanese comments
if(preg_match($pattern, $incoming_comment['comment_content'])) {
wp_die( "Comments are not allowed to contain Japanese!" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'inlojv_comment_post');
// Block spam comments END
In addition, if we find that some users are manually replying, but their comments on each visit are meaningless "互访啊、支持、或者是直接留链接" (mutual visit, support, or directly leave a link), we can put these types of replies into spam comments first.
Set it in "Dashboard -> Settings -> Discussion -> Comment Blacklist". Add the website of these users (gambling websites are given priority), email, and IP to this list, so that when the user leaves a comment next time, it will automatically go into the spam comments.
In addition, there are also sliding unlock and some captcha methods available online, which can effectively block spam comments. However, they are not very user-friendly, so they are not recommended. Additionally, the spam comment plugin Akismet provided by WordPress is also very powerful. However, some netizens have reported that after enabling this plugin, comments will become slightly slower. Finally, you can choose according to your personal needs.
I come from China and I am a freelancer. I specialize in Linux operations, PHP, Golang, and front-end development. I have developed open-source projects such as Zdir, ImgURL, CCAA, and OneNav.