倡萌在《WordPress 如何有效拦截和过滤垃圾评论》已经分享了多种垃圾评论拦截方法,这篇文章是对那篇文章的方法的进阶。
评论中禁止包含指定内容
以下代码禁止许评论中包含 <a 随便 href=" 或者rel="nofollow"或者http:// ,你也可以根据自己的需要修改:
function lianyue_comment_post( $incoming_comment ) {
$http = '/[href="|rel="nofollow"|http:\/\/|<\/a>]/u';
if(preg_match($http, $incoming_comment['comment_content'])) {
wp_die( "万恶的发贴机!" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'lianyue_comment_post'); |
function lianyue_comment_post( $incoming_comment ) {
$http = '/[href="|rel="nofollow"|http:\/\/|<\/a>]/u';
if(preg_match($http, $incoming_comment['comment_content'])) {
wp_die( "万恶的发贴机!" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'lianyue_comment_post');
评论中必须包含指定内容
比如下面的代码就是评论必须包含中文字符,你可以修改为自己指定的字符:
function lianyue_comment_post( $incoming_comment ) {
$pattern = '/[一-龥]/u';
// 禁止全英文评论
if(!preg_match($pattern, $incoming_comment['comment_content'])) {
wp_die( "您的评论中必须包含汉字!" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'lianyue_comment_post'); |
function lianyue_comment_post( $incoming_comment ) {
$pattern = '/[一-龥]/u';
// 禁止全英文评论
if(!preg_match($pattern, $incoming_comment['comment_content'])) {
wp_die( "您的评论中必须包含汉字!" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'lianyue_comment_post');
评论中禁止或必须包含指定内容
这是上面两种方法的综合,评论必须包含中文字符,且禁止包含 <a 随便 href=" 或者rel="nofollow"或者http://
function lianyue_comment_post( $incoming_comment ) {
$pattern = '/[一-龥]/u';
$http = '/[href="|rel="nofollow"|http:\/\/|<\/a>]/u';
// 禁止全英文评论
if(!preg_match($pattern, $incoming_comment['comment_content'])) {
wp_die( "您的评论中必须包含汉字!" );
}elseif(preg_match($http, $incoming_comment['comment_content'])) {
wp_die( "万恶的发贴机!" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'lianyue_comment_post'); |
function lianyue_comment_post( $incoming_comment ) {
$pattern = '/[一-龥]/u';
$http = '/[href="|rel="nofollow"|http:\/\/|<\/a>]/u';
// 禁止全英文评论
if(!preg_match($pattern, $incoming_comment['comment_content'])) {
wp_die( "您的评论中必须包含汉字!" );
}elseif(preg_match($http, $incoming_comment['comment_content'])) {
wp_die( "万恶的发贴机!" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'lianyue_comment_post');
修复ajax评论使用本代码出现的错位
如果你的主题使用ajax评论,使用上面的代码后,可能在提示错误信息的时候,网站会错位,你可以打开 comments-ajax.php ,找到最后个err( __(并且跳过该行,在下一行增加:
这是禁止包含的内容
$http = '/[href="|rel="nofollow"|http:\/\/|<\/a>]/u';
if (preg_match($http,$comment_content) )
err( __('万恶的发贴机!') ); |
$http = '/[href="|rel="nofollow"|http:\/\/|<\/a>]/u';
if (preg_match($http,$comment_content) )
err( __('万恶的发贴机!') );
这是必须包含中文的
$pattern = '/[一-龥]/u';
if (!preg_match($pattern,$comment_content) )
err( __('您的评论中必须包含汉字!') ); |
$pattern = '/[一-龥]/u';
if (!preg_match($pattern,$comment_content) )
err( __('您的评论中必须包含汉字!') );
原文:http://www.lianyue.org/2011/2107/
来源:
https://www.wpdaxue.com/wordpress-spam-comments.html
微信公众号
手机浏览(小程序)
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(): Failed to enable crypto in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_33253.jpg): failed to open stream: operation failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57