How to limit Commenting in WordPress to one Comment per User

Each post or page in WordPress has got an option for comments. That is a handy feature, and if you are developing your custom project and don’t need comments, you can use it for some other feature. You can use the comments function as offers, reviews etc. We had the same situation while developing a custom feature for a client and had to limit it to one comment per user.

There is no built-in feature in WordPress to limit the number of comments per user. Nor on the other hand, is there a plugin that we found. However, limiting it is not that hard. All it takes a few lines of code that you can insert into your functions.php file.

How to limit commenting to one comment per user per post or page in WordPress

As we already said, it can be done by adding a simple code to your functions.php file. Go to your admin dashboard and click on Appearance > Theme Editor.

Wordpress Appearance and Theme Editor

Once you are in your theme editor, click on the functions.php file on the right. Then, add the following code right at the end of the file:

add_filter('comments_open', 'restrict_users', 10, 2);

function restrict_users($open, $post_id) {
   if (intval($post_id) && get_post($post_id)) {
       $args = array('post_id' => $post_id, 'count' => true);
       $user = wp_get_current_user();
       if ($user && intval($user->ID)) { // for registered users
           $skip = false;
           $ignoreTheseRoles = array('administrator', 'editor'); // which user roles should be ignored
           if ($user->roles && is_array($user->roles)) {
               foreach ($user->roles as $role) {
                   if (in_array($role, $ignoreTheseRoles)) {
                       $skip = true;
                       break;
                   }
               }
           }
           if (!$skip) {
               $args['user_id'] = $user->ID;
               $open = get_comments($args) ? false : true;
           }
       } else { // for guests
           $commenter = wp_get_current_commenter();
           if ($commenter && is_array($commenter) && isset($commenter['comment_author_email'])) {
               $args['author_email'] = $commenter['comment_author_email'];
               $open = get_comments($args) ? false : true;
           }
       }
   }
   return $open;
}

So, let’s analyse what have we written. As you can see, we have split the code into two parts. The first part is checking the registered users, and the second one is for guests. Probably the most optimal option for using this solution is to limit commenting to users only, however if you allow guests, it can be solved as well by checking the author’s e-mail.

If you do not allow guest commenting, then you can ignore lines 22 – 28. As for the registered users, on the line 9 we have an option to ignore limit for particular roles (in our case “administrator” and “editor”). You can change that yourself by editing the $ignoreTheseRoles variable.

And that’s basically it. Click on “Update”, and your comments should be limited. You can play with this code even further and filter it to only particular categories, post IDs etc.

Follow us for more

We hope you liked our article, and if you have any questions or comments, please leave it in the comment section under the article. Feel free to explore our other WordPress tutorials for your blog. For more news follow us on our Facebook and Twitter profiles.

Leave a Reply

Your email address will not be published.