WordPress 限制一段时间内用户的最大评论数量

wp教程11个月前更新 知道君
96 0 0

如果一段时间内用户有大量的评论,基本可以认为是垃圾评论,使用 wordpress 对其进行限制,可以将以下代码添加到 function.php。

10 分钟内最多评论 10条,代码如下:

// 10 分钟评论不能大于 10 条
function limit_user_comments_per_ten_minutes( $comment_data ) {
    global $wpdb;

    $current_user = wp_get_current_user();
    $user_id = $current_user->ID;

    $count = $wpdb->get_var( $wpdb->prepare(
        "SELECT COUNT(*) FROM $wpdb->comments
        WHERE user_id = %d
        AND comment_date > DATE_SUB(NOW(), INTERVAL 10 MINUTE)",
        $user_id
    ) );

    if ( $count >= 10 ) {
        wp_die( '您已经达到了10分钟内评论的最大限制。' );
    }

    return $comment_data;
}

add_filter( 'preprocess_comment', 'limit_user_comments_per_ten_minutes' );

一小时内最多评论 10条,代码如下:

function limit_user_comments_per_hour( $comment_data ) {
    global $wpdb;

    $current_user = wp_get_current_user();
    $user_id = $current_user->ID;

    $count = $wpdb->get_var( $wpdb->prepare(
        "SELECT COUNT(*) FROM $wpdb->comments
        WHERE comment_author_email = %s
        AND comment_date > DATE_SUB(NOW(), INTERVAL 1 HOUR)",
        $comment_data['comment_author_email'],
        $_SERVER['REMOTE_ADDR']
    ) );

    if ( $count >= 20 ) {
        wp_die( '您已达到每小时评论的最大限制。' );
    }

    return $comment_data;
}

 

© 版权声明

相关文章

暂无评论

暂无评论...