WordPress检测评论函数:check_comment
WordPress函数check_comment用于检测评论各字段,以判断是否为恶意评论。
check_comment( string $author, string $email, string $url, string $comment, string $user_ip, string $user_agent, string $comment_type ): bool
函数参数
$author
字符串
评论作者名称
字符串
评论作者邮箱
$url
字符串
评论作者网址
$comment
字符串
评论内容
$user_ip
字符串
评论作者IP地址
$user_agent
字符串
评论作者User-Agent
$comment_type
字符串
评论类型,comment、trackback、pingback
函数返回值
如果所有条件都检查通过,返回true,否则返回false。
在WordPress后台讨论设置中,可以设置一些检测条件:
- 如果勾选了“评论必须经人工批准”,无论其他条件如何都将返回false;
- 如果链接数超过设置的数量,则返回false;
- 如果任何参数内容包含不允许的单词,则返回false;
- 如果评论作者之前已获得批准,则评论将自动获得批准;
函数使用示例
<?php
$author = "背字根";
$email = "beizigen@qq.com";
$url = "https://www.beizigen.com";
$comment = "这是一条评论...";
$user_ip = "1.1.1.1";
$user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.79 Safari/535.11";
$comment_type = "comment";
if ( check_comment( $author, $email, $url, $comment, $user_ip, $user_agent, $comment_type ) ) {
echo "评论通过检测";
} else {
echo "评论未通过检测";
}
?>
扩展阅读
check_comment()函数位于:wp-includes/comment.php
相关函数:
- wp_allow_comment()