How to Disable and Hide Comments on Posts of specific categories

In this tutorial I will share a simple snippet which allows you to disable / hide comments on Blog Posts of selected categories.

The snippet is quite simple and does two things

1) It first checks if the post belongs to disabled categories. The check is done after the post object is completely created.

2) In case the post belongs to disabled category, the code uses comments_open() filter to close the comments on the particular post.

Here is the snippet.

Simply paste it to your functions.php file and you will be good to go

add_action( 'the_post', 'st_check_for_closed' );

    
  function st_check_for_closed()
    {

    global $post;

    $my_post_cat = wp_get_post_categories($post->ID);
   
                
    $disabled_cat = array( "1", "3"); // this is he array of disabled categories. Feel free to edit this line as per your needs. 
          

    $my_result = array_intersect($my_post_cat,$disabled_cat);
 
        if (empty ( $my_result ) ) 
                      {
            return; 
                       }

         else { 
               add_filter( 'comments_open', 'st_close_comments_on_category', 10, 2 );
               add_action('wp_enqueue_scripts', 'st_deregister_reply_js');
         
             }
    }

          function st_deregister_reply_js() 
        {
        wp_deregister_script( 'comment-reply' );

        }

                
      function st_close_comments_on_category ($open, $post_id) 
        {
            $open = false;
        }

Here is an Explanation of the logic behind the script.

1) Hook into the_post action:

According to the codex, the_post allows us to modify the post object  after it has been setup. We are hooking a custom function st_check_for_closed which checks if the post belongs to a disabled category.

2) Get the post categories:

We will use the function wp_get_post_categories to get the categories to which the post belongs.

3) Check if post belongs to disabled category

We are using array_intersect function to check if there is any intersection between the $my_post_cat and $disable_cat array. If there is an intersection , e can safely assume that the post belongs to one or more disabled categories.

4) Run appropriate filter:

In case the post belongs to a disabled category we will close the comments by hooking to the comments_open filter.

We will hook two functions, st_close_comments_on_category and st_deregister_reply_js.  The role of these functions is to mark comments as closed and De-register the comment-reply.js respectively.

Well thats it. This snippet is quite straight forward. Simply paste it in your functions.php file and you are good to go.

Use a Plugin

Disable Comments on Post Categories ScreenShot

For those of you who are not comfortable with editing code, I recently released a plugin to do the same.

Go to Settings -> Disable Comments on Post Categories and check the categories for which you want to disable comments.

Thats it. The plugin will now ensure that comments are closed on Posts of disabled categories.

If you have any questions then feel free to ask in the comments section 🙂

Did you enjoy this article?
We regularly publish WordPress related Tutorials and Tips on this blog. Signup today and receive free updates straight in your inbox.
I agree to have my personal information transfered to Mad Mimi ( more information )
About Ankit

Founder of SpiceWp. We share useful Wordprss Tips and Tricks.

I have also released 17 Wordpress Plugins till date .

I regularly publish Wordpress related Tutorials and Tips on this blog.
If you like the content then consider subscribing to the blog here.

Comments

  1. Thank you, it really works without installing as plugin

  2. It really worked. Thanks a lot!

Speak Your Mind

*