How to check if comments are allowed in WordPress

There are two ways that plugin and theme authors can check whether comments are allowed on a post in WordPress:

Old-school way (wrong unless you have a very good reason):

if( $post->comment_status == 'open' ) {
	// do something here
}

Correct way:

if( comments_open() ) {
	// do something here
}

The second way is better because the comments_open() function is filterable — other plugins and themes can alter its output depending on context.

What makes WordPress so powerful is the underlying API. Plugin and theme authors can modify almost anything, and this is why there are so many thousands of plugins and themes out there. But we plugin and theme authors need to make sure we return the favour, so that our themes/plugins can play nicely with the rest. So, if your plugin or theme is using the first method above, please change it to use the second.

One other note for developers: WordPress has for some time now supported arbitrary post types (not just the posts and pages that we’re all used to), some of which don’t necessarily support comments at all. So it’s a good idea to check whether a post type even supports comments before running any comment-specific code:

if( post_type_supports( get_post_type(), 'comments' ) ) {
	// put comment related code here, including...
	if( comments_open() ) {
		// ...a comment form, maybe
	}
}
else {
	// sit back and relax
}

The code beneath the default WordPress theme uses both of the functions mentioned above.