Hack: Override Comment Deadline For Some Posts

If you want to disable comments for a post, this is pretty easy to do in the standard WordPress admin interface. You can also set a deadline in Settings, Discussions to have comments automatically close after so many days on all posts. But what if you wanted to override that deadline for a specific post or posts?

A simple bit of code will work to do this. All you need to do is to use the comments_open hook. If you return a true from this function, then comments will be open. So you can check to see if the post is one that matches a particular post ID or title. You can also easily check custom post fields.

add_action('comments_open', 'wpaction_override_comment_disable');
function wpaction_override_comment_disable($enabled) {
	global $post;
	// see if a custom field comment_disable is present and set to 'override' for this post.
	$custom_fields = get_post_custom($post->ID);
	if ($custom_fields['comment_disable'][0] == 'override') return true;
	// see if this is post number X
	if ($post->ID == 16) return true;
	return $enabled;

This code example demonstrates both. It allows posts with the custom field comment_disable set to override. It also allows the post with ID 16. So you can choose how you want it to work. I like the custom field idea because they are relatively easy to add and then change again without needing to edit this code. You can place this code in your functions.php or custom plugin. Notice that the real value of whether comments are enabled is passed in as a parameter, and then passed out again if none of the conditions are met.

Leave a Reply

Your email address will not be published. Required fields are marked *