评论状态审核钩子:pre_comment_approved
描述
在将评论插入数据库之前,由 wp_allow_comment 函数调用的过滤器钩子。 该钩子适用于改变评论的审批状态,允许插件覆盖。
用法
<?php function filter_handler( $approved , $commentdata ){ ...... }
add_filter( 'pre_comment_approved' , 'filter_handler' , '99', 2 ); ?>
参数
$approved
(混合型)(必需)初步评论批准状态:0, 1, 'trash', or 'spam'。
默认值:无
$commentdata
(数组)(必需)评论相关数据数组,跟 get_comments()函数返回的数据相似,详细如下:
'comment_post_ID' – 评论文章 ID
'comment_author' – 评论者(可能为空)
'comment_author_emAIl' – 评论者邮箱(可能为空)
'comment_author_url' – 评论者网址(可能为空)
'comment_author_IP' – IP 地址
'comment_agent' – 评论者 UA,e.g., "Mozilla/5.0…"
'comment_content' – 评论内容
'comment_type' – 评论类型:'pingback', 'trackback', or empty for regular comments
'user_ID' – 评论者用户 ID(访客的 ID 为空)
默认值:无
返回值
0 (int): 评论状态为"Pending" (待审核)
1 (int) : 评论状态为"Approved" (通过审核)
'spam' (string): 评论被标记为"垃圾评论"
'trash' (string): 评论被转移至"垃圾箱"
一般说来,评论被标记为“spam”或“trash”的话,则不会对外展示~
示例
好了,钩子介绍完了,下面就来几个具体的例子以供参考~
自动将包含长链接的评论标记为垃圾评论
/**
* WordPress控制评论状态的钩子:pre_comment_approved - 龙笑天下
* https://www.ilxtx.com/wordpress-filter-pre-comment-approved.html
* 实用方法:新增评论规则 -- 晚上23:30-9:00的评论全部设为待审核
*/
function lxtx_rkv_url_spamcheck( $approved , $commentdata ) {
return ( strlen( $commentdata['comment_author_url'] ) > 50 ) ? 'spam' : $approved;
}
add_filter( 'pre_comment_approved', 'lxtx_rkv_url_spamcheck', 99, 2 );
登陆用户的评论无需审核
/**
* WordPress控制评论状态的钩子:pre_comment_approved - 龙笑天下
* https://www.ilxtx.com/wordpress-filter-pre-comment-approved.html
* 实用方法:新增评论规则 -- 晚上23:30-9:00的评论全部设为待审核
*/
function lxtx_loggedin_approved_comment($approved){
// No need to do the check if the comment is already approved anyway.
if (!$approved) {
if (is_user_logged_in()) {
// Note: 1/0, not true/false
$approved = 1;
}
}
return $approved;
}
// Action allows the comment automatic approval to be over-ridden.
add_action('pre_comment_approved', 'lxtx_loggedin_approved_comment');
特定文章/页面评论必须审核
用户在特定文章或页面的发表评论,评论必须审核,但管理员和文章作者的评论可以自动通过审核,假设该文章/页面的 ID 是 112。
/**
* WordPress控制评论状态的钩子:pre_comment_approved - 龙笑天下
* https://www.ilxtx.com/wordpress-filter-pre-comment-approved.html
* 实用方法:新增评论规则 -- 晚上23:30-9:00的评论全部设为待审核
*/
function lxtx_page_approved_comment($approved, $commentdata){
$post = get_post( $post );
if ( !empty($post->ID) && $post->ID == 112 ) {
$approved = 0;
if( $user_id = $commentdata['user_id'] ){
$user = get_userdata( $user_id );
if ( $user_id == $post->post_author || $user->has_cap( 'moderate_comments' ) ){
// The author and the admins get respect.
$approved = 1;
}
}
}
return $approved;
}
// Action allows the comment automatic approval to be over-ridden.
add_action('pre_comment_approved', 'lxtx_page_approved_comment', 10, 2);
晚上 23:30-9:00 的评论全部设为待审核
下面就来介绍前文说的这个方法了~ 管理员可以无视该规则~
/**
* WordPress控制评论状态的钩子:pre_comment_approved - 龙笑天下
* https://www.ilxtx.com/wordpress-filter-pre-comment-approved.html
* 实用方法:新增评论规则 -- 晚上23:30-9:00的评论全部设为待审核
*/
function lxtx_limit_comment_to_pending($approved, $commentdata){
date_default_timezone_set('Asia/Shanghai'); //设置为东八区上海时间
$time = time();
$t1 = date("Y-m-d",$time).' 09:00:00';
$t2 = date("Y-m-d",$time).' 23:30:00';
$short_t1 = strtotime($t1);
$short_t2 = strtotime($t2);
if( ($time>$short_t2 || $time<$short_t1) && !current_user_can('manage_options') ){
$approved = 0;
}
return $approved;
}
add_action('pre_comment_approved', 'lxtx_limit_comment_to_pending', 10, 2);
代码中涉及了时间的格式,具体的可以想见下面这篇文章~

评论(0)