Programmatically Turn Off Comments in WordPress with Filter

To turn comments off programmatically with a filter in WordPress, you can set the post object’s comment_status variable to “closed”. Call the filter at some point after the post is loaded but before the comments are rendered. This is a hack, but I haven’t seen a simpler approach, beside installing another plugin. In the example below, the condition to disable comments is simple. If a post is in a certain category, comments aren’t allowed. Otherwise, go ahead and behave as normal. You can still disable comments on a per post basis in other categories.

One thing to note is the use of the global $post object. The function needs access to the object to set the variable.

add_filter('get_header', 'sb_turn_comments_off');
function sb_turn_comments_off(){
    if(in_category("Projects") AND is_single() ){
      global $post;
      $post->comment_status="closed";
  }
}

I’ve only tested this with the Thematic framework. I assume that other themes will check the comment_status variable before allowing comments.

  • http://www.sixprizes.com/ Adam

    YOU ARE THE MAN!

    I came up with another way to disable comments by category with a filter, but it wasn’t quite working because I use Disqus comments. This is working perfectly though!

    Here is the code I was using, just for reference:

    add_filter( 'comments_open', 'falso_commento', 1 );
    function falso_commento() {
    if ( in_category('underground') )
    return false;
    else
    return true;
    }

  • http://binarym.com/ matt mcinvale

    hack or not, it works perfectly for me.

  • Olivia

    Thank you, I tried several other suggestions but this one finally worked!

    • Olivia

      Orrrrrrr I spoke too soon. Sigh…

      • bseanvt

        what didn’t work? use paste.org and i’ll take a look at the code

        • http://thegeekamites.webege.com/wp/ Olivia

          I used the exact code you provided (of course changing the category name) in my functions.php. Something interesting is happening now though. My homepage displays one comic (I use Comicpress, and the comics are where I want to have no comments), and right now there is an option to comment on the homepage comic despite using your code. However, when I click the comment link, it just brings me to the page of the post with comment options nowhere to be seen.

          • bseanvt

            that code assumes you’re on the single.php view. the call to is_single() is where that happens. you could try is_home() or is_front_page() functions as well… or just get rid of the is_single() function entirely and just use in_category(“Category Name”) …also, you can edit your theme templates to take out the html. have a pastie i can look at?

          • bseanvt

            i looked at your site. that may be a hard coded link in the loop in your index.php file. the code above won’t take anything out if it’s hard coded as html. only if it’s being programmatically generated.?