Jump Menu : Content | Explore | Comments | Search | Home | Sitemap | Contact | Login | Access.

WordPress Tip: Disable Comments in Old Posts via PHP

Just a quick WordPress snippet for future reference. I recently explained how to disable comments, pingbacks, and trackbacks via SQL. Here’s a good way to do it via PHP:

<?php 
function close_comments( $posts ) {
	if ( !is_single() ) { return $posts; }
	if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( 30 * 24 * 60 * 60 ) ) {
		$posts[0]->comment_status = 'closed';
		$posts[0]->ping_status    = 'closed';
	}
	return $posts;
}
add_filter( 'the_posts', 'close_comments' ); 
?>

You can run this script as a plugin, through your theme’s functions.php, or through a custom user-functions.php file. Simply set the desired number of days by changing the number “30” to whatever you would like. As is, this script will close comments, pingbacks and trackbacks on all articles posted more than 30 days ago.

Related articles

About this article

This is article #578, posted by Jeff Starr on Tuesday, July 08, 2008 @ 07:43am. Categorized as WordPress, and tagged with comments, php, pingbacks, posts, sql, tips, trackbacks, tricks, WordPress. Updated on . Visited 5449 times. 4 Responses »

BookmarkTrackbackCommentSubscribeExplore

« Working with Multiple Themes Outside of the WordPress Installation Directory • Up • Provide a Link for Visitors to Verify Your Feedburner Subscriber Count »


4 Responses

1 • July 8, 2008 at 5:22 pm — Nikhil G. says:

Hi perishable..

:D when i came at your site, for Apathy theme. It was quite absurd to see the blank page returned when you try to publish a comment.

So, is there a way to use this script and if comments are closed for that particular post, then a “NO COMMENTS ON OLD POST” message or something like that is displayed. ??

It become quite confusing else.

Thanks,
Nikhil G.

2 • August 4, 2008 at 8:15 am — Jeff Starr says:

Hi Nikhil, somehow I missed this comment when it was first posted. I will look into the Apathy theme and see if I can figure out what’s happening with the comments.
Regards,
Jeff

3 • August 5, 2008 at 7:06 pm — Jeff Starr says:

Nikhil, I just finished testing the Apathy theme. Everything seems to be working great. Commenting works perfectly on posts with open comments; posts with closed comments display a clear message indicating that comments are closed.

Looking at the date that you left the comment on this post, I think my server may have been causing the problem. Every now and then a blank white page appears after posting a comment. This is an issue with my web host and in no way represents the functionality of any of my themes.

I hope this helps resolve any confusion and/or frustration that you might have experienced while using the site. Please let me know if I may be of any additional assistance with this matter.

Regards,
Jeff

Drop a comment


Trackbacks / Pingbacks

  1. JasonMorrison.net » How to keep spam off your blog, bulletin board, or forum

Set CSS to lite theme
Set CSS to dark theme