skip to Main Content

WordPress Code Snippets


How to Limit Comments Per User in WordPress for Better Control

Restrict the number of comments a user can post.

add_action('preprocess_comment', 'codebykp_limit_comments_per_user');
function codebykp_limit_comments_per_user($commentdata) {
    $user_id = get_current_user_id();
    if ($user_id) {
        $comments = get_comments(array('user_id' => $user_id));
        if (count($comments) >= 5) { // Set limit
            wp_die('You have reached the limit of comments you can post.');
        }
    }
    return $commentdata;
}


I’m a WordPress developer with 10+ years of experience in WooCommerce and custom plugins. I combine technical expertise with design flair to help you create standout, user-friendly websites. Let’s transform your digital presence!

Best WordPress Hosting
Back To Top