now available

now available
Want fresh tech tips in your inbox?

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;
I've only tested this with the Thematic framework. I assume that other themes will check the comment_status variable before allowing comments.

Tagged w/ #comments #content #global #plugin #post #programmatically #thematic #wordpressprogramming

Apps I've Built

XPath Expression Editor
Practice and improve your XPath skills with XPath Editor
Click to buy on the App Store
Photo Location Changer
Easily change the location on your photos and videos
Click here for more info about the app
Photo Date Changer
Easily change the dates and times on your photos and videos
Click here for more info about the app
English dictionary with notifications so you won't forget what you're studying!
Click here for more info about the app
The app that quizzes and scores you on your vocabulary!
Click here for more info about the app