wordpress教程

WordPress评论必须中文+禁止外链方法

我的站长站 2023-08-03 人阅读

必须包含中文

防止灌水,判断评论内容是否包含中文,不能包含其他语言文字。

// 评论必须含中文
function wp_refused_spam_comments($comment_data) {
$pattern = '/[一-龥]/u';
$jpattern = '/[ぁ-ん]+|[ァ-ヴ]+/u';
if (!preg_match($pattern, $comment_data['comment_content'])) {
err(__('评论中需要有一个汉字!'));
}
if (preg_match($jpattern, $comment_data['comment_content'])) {
err(__('不能有日文!'));
}
return ($comment_data);
}
add_filter('preprocess_comment', 'wp_refused_spam_comments');

禁止带外链

防止发外链的评论,自然就断绝了很多想灌水发推广的人的脚步。

//禁止发链接
function wp_comment_post( $incoming_comment ) {
$http = '/[href="|rel="nofollow"|http:\/\/|<\/a>]/u';
if(preg_match($http, $incoming_comment['comment_content'])) {
err( "禁止发链接地址!" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'wp_comment_post');

使用方法

把以上两段代码添加到当前主题模板的functions.php文件中就可以了。

如何你觉得修改functions.php文件太麻烦,推荐你使用Code Snippets插件来代替。


wordpress教程标签