给wordpress添加评论过滤器,如果用户留言包含 "http" (可以为任意字符串) 就禁止提交评论。
function filter_comment_content($comment_data) {$comment_contents = $comment_data["comment_content"]; //获取评论表单的内容字段if (stripos($comment_contents,'http') !== false){ // 如果评论内容包含 "http",则禁止提交评论wp_die('抱歉,评论内容包含不允许的链接。请删除链接后重新提交评论');}return $comment_data;
}
add_filter('preprocess_comment', 'filter_comment_content'); // 添加评论过滤器到 WordPress
strpos() :这个函数是大小写敏感的,意味着它会考虑子字符串的大小写。
stripos() : 这个函数是大小写不敏感的,它会忽略子字符串的大小写。
匹配多个关键词,如果包含其中一个就禁止提交
function filter_comment_content($comment_data) { $searchString = $comment_data["comment_content"]; //获取评论表单的内容字段$keywords = array("http", "nihao","www");// 使用in_array函数来检查字符串是否包含数组中的任意一个值foreach ($keywords as $keyword) {if (strpos($searchString, $keyword) !== false) {wp_die("<h1>I'm sorry,</h1><a href=''>home</a>");}}//如果没有找到匹配的关键词return $comment_data;}
add_filter('preprocess_comment', 'filter_comment_content'); // 添加评论过滤器到 WordPress