有朋自远方来,不亦乐乎。
关于WordPress控制评论状态的多种玩法
原创 橘子书  18 Feb/02 660 A+

自动将包含长链接的标记为垃圾评论

function lyt_url_spamcheck( $approved , $commentdata ) {
    return ( strlen( $commentdata['comment_author_url'] ) > 50 ) ? 'spam' : $approved;//50代表字符大于50
}
add_filter( 'pre_comment_approved', 'lyt_url_spamcheck', 99, 2 );

登陆用户的评论无需审核

function lyt_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', 'lyt_loggedin_approved_comment');

特定文章/页面评论必须审核

function lyt_page_approved_comment($approved, $commentdata){  
    $post = get_post( $post );
    if ( !empty($post->ID) && $post->ID == 112 ) {//112代表指定的页面或文章ID
        $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', 'lyt_page_approved_comment', 10, 2);

晚上23:30-9:00的评论全部设为待审核

function lyt_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', 'lyt_comment_to_pending', 10, 2);
//来自https://mkblog.cn/
function lyt_limit_comment_to_pending($approved, $commentdata){
    date_default_timezone_set('Asia/Shanghai'); //设置为东八区上海时间 
    if(!current_user_can('manage_options')) {
    if((date('G') < 9) || (date('G') > 23)) $approved = 0; // 23:00~9:00 待审核 
    } 
    return $approved;
}
add_action('pre_comment_approved','lyt_limit_comment_to_pending', 10, 2);

根据文章发布的时间来自动关闭文章的评论功能

function lyt_close_comments( $posts ) {
    if ( !is_single() ) { return $posts; }
    if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( 30 * 24 * 60 * 60 ) ) {
        $posts[0]->comment_status = 'closed';
        $posts[0]->ping_status    = 'closed';
    }
    return $posts;
}
add_filter( 'the_posts', 'lyt_close_comments' );

根据文章的评论数量来自动关闭文章的评论功能

function lyt_disable_comments( $posts ) {
    if ( !is_single() ) { 
        return $posts; 
    }
    if ( $posts[0]->comment_count > 100 ) {//100代表评论数大于100条
        $posts[0]->comment_status = 'disabled';
        $posts[0]->ping_status = 'disabled';
    }
    return $posts;
}
add_filter( 'the_posts', 'lyt_disable_comments' );

一键关闭/开启Wordpress评论功能

function close_open_comments( $posts ) {
    $postids = array('110','119');//110跟119代表要过滤的页面跟文章
    if ( !empty( $posts ) && is_singular() && !in_array($posts[0]->ID,$postids) ) {
        $posts[0]->comment_status = 'closed';
        $posts[0]->post_status = 'closed';
    }
    return $posts;
}
add_filter( 'the_posts', 'close_open_comments' );

根据评论数量来判断是否显示评论者链接

function lyt_is_friend( $email = null , $num = 5 ){//5代表控制显示的数量
    $count = get_comments(array(
        'author_email' => $email,
        'count' => true,
    ));
    return ( $count > $num );
}
 
function lyt_update_friend_list( $comment_id ){
    $comment = get_comment($comment_id);
    $friend_list = get_option('friend_list') ? get_option('friend_list') : array();
    $email = $comment->comment_author_email;
    if ( lyt_is_friend($email) && !in_array( $email , $friend_list) ) {
        $friend_list[] = $email;
        update_option('friend_list',$friend_list);
    }
}
add_action('comment_post', 'lyt_update_friend_list');
 
function lyt_show_friend_link( $return , $author, $comment_ID ){
    $comment = get_comment( $comment_ID );
    $email = $comment->comment_author_email;
    $friend_list = get_option('friend_list') ? get_option('friend_list') : array();
    if ( in_array($email,$friend_list) ) {
        return $return;
    } else {
        return $author;
    }
}
add_filter('get_comment_author_link','lyt_show_friend_link',10,3);//适用使用the_author_link()来输出评论者昵称的主题

文章代码来自龙笑天下,有不懂的地方,请直接戳他!!!

龙笑天下

阅读全文

该文章还没有任何评论,你来说两句吧.

发表评论

莫慌,小狮子正在努力加载评论...