Like the blog? Get the book »

Leave Comments Open Forever on Specific Categories or Page Templates

Leave Comments Open Forever on Specific Categories or Page Templates

I like the idea of shutting off comments after a certain number of days. Here on Digging Into WordPress we do it after 90. After that kind of time, the “community” of the discussion is long over. I think a good practice for turning off comments is to instead leave a message informing visitors that the comment thread is closed, and offer a course of action in case they have something of grave importance to share. Here is an example from CSS-Tricks:

Comments-closed message at CSS-TricksThe “comments closed” message on CSS-Tricks

Doing this is as simple as going to Settings ▸ Discussion in your Admin area and setting the number of days before comments are turned off. This setting is completely global in that any single place on your entire site where comments are enabled, they will close after this number of days. On CSS-Tricks, I decided that while I like closing comments on articles after a certain amount of days, I wanted to leave comments open forever on my “snippets”, as I thought that those really aren’t meant for lengthy discussions anyway, and the comments are more for people sharing what works and what doesn’t. There really is no need for comments to be turned on for those.

Unfortunately I had no idea how to accomplish such a thing. So I emailed the smart guys over at WP Engineer, who came up with some good stuff.

Leave comments open forever on a particular CATEGORY

This is the code in it’s original form from WP Engineer:

/**
 * Close comments on an old post.  Hooked to comments_open and pings_open.
 * small modifications on the default functions of WordPress
 *
 * @param bool $open Comments open or closed
 * @param int $post_id Post ID
 * @param int $cat Category ID (cat_ID), set '' or empty for all categories
 * @param int $days_old Days to close comments, set '' for use value from settings
 * @return bool $open
 */
function wpe_close_comments_for_old_post( $open, $post_id, $cat = array(1, 4), $days_old = '' ) {
	if ( !$open )
		return $open;
 
	if ( in_category($cat) )
		return $open;
 
	if ( '' === $days_old )
		$days_old = (int) get_option('close_comments_days_old');
 
	if ( !$days_old )
		return $open;
 
	$post = get_post($post_id);
 
	if ( time() - strtotime( $post->post_date_gmt ) > ( $days_old * 24 * 60 * 60 ) )
		return false;
 
	return $open;
}
add_filter( 'comments_open', 'wpe_close_comments_for_old_post', 10, 2 );
add_filter( 'pings_open',    'wpe_close_comments_for_old_post', 10, 2 );

Leave comments open forever for a particular PAGE TEMPLATE

My own needs were slightly different in that I needed this to work for a special page template not a specific category (my snippets use page/parent structure not posts). I also just wanted regular posts to default to whatever setting was in the Admin, not set that number from the function.

And so my modified version is:

function wpe_close_comments_for_old_post( $open, $post_id, $template = 'page-snippet.php' ) {

	global $wp_query;
	
	if ( !$post_id ) {
		$post_id = $wp_query->post->ID;
    }
 
	if ( get_post_meta($post_id, '_wp_page_template', true) == $template && get_option('close_comments_for_old_posts') ) {
		return true;
	}

	return $open;
	
}
add_filter( 'comments_open', 'wpe_close_comments_for_old_post', 10, 2 );
add_filter( 'pings_open',    'wpe_close_comments_for_old_post', 10, 2 );

Works like a charm!

2 responses

  1. hi there!

    been searching the net for something like this…

    I can’t get it to work… what i wanted was to keep all my comments open FOREVER…

    where exactly did you insert this code?

    and may be perhaps you could help me on the comment open forever thingie…

    thanks! appreciate it…

Comments are closed for this post. Contact us with any critical information.
© 2009–2024 Digging Into WordPress Powered by WordPress Monzilla Media shapeSpace