
To prevent spammers from flooding old articles with useless comments, you can set WordPress to close comments after a certain number of days:
It might be surprising for some users if the comments are closed automatically, so it might be a good idea to inform them about the remaining time.
add_action( 'comment_form_top', 'topic_closes_in' ); function topic_closes_in() { global $post; if ($post->comment_status == 'open') { $close_comments_days_old = get_option( 'close_comments_days_old' ); $expires = strtotime( "{$post->post_date_gmt} GMT" ) + $close_comments_days_old * DAY_IN_SECONDS; printf( __( '(This topic will automatically close in %s. )', 'domain' ), human_time_diff( $expires )); } }
While the code should be almost self explanatory there is an interesting function not every WordPress developer might know: human_time_diff()
. This function is hidden in the .../wp-includes/formatting.php
file. It is originally planned to be used in themes to provide a more “human” touch to the date/time a post was written. Since the function does not care if the date is in the past or in the future we can use it for our needs.

Comments
14 responses to “Inform user about automatic comment closing time”
Very useful snippet, thanks for sharing!
I added it immediately to my Code Snippets library
Wow, these are awesome! I am following this rules on my site for old articles. It really helped me…. thank you latz.
A very useful tip indeed. Thanks for sharing.
Though I’m not sure of how good the overall concept of closing comments is, in a sense that the community building and engagement with new users becomes somewhat more difficult.
To me, it looks like a workaround of spamming issue, not a solution.
great tip – it might also encourage visitors to immediately leave a comment. as always if there is a near deadline coming people are more engaged as they feel they could miss something.
for me this is a quite advanced level but it makes really sense from the usability point of view.
Regards from Vienna
yr. Startup
Very useful info.
Off-topic… Can I use Xtreme One Framework for commercial purposes?
Closing comment is not solution in community site to stop spam. I think nofollow tag is only solution to stop spamming.
I have idea for good post ๐ Write article about creating ajax comment submit with/without a plugin
Informing users about the comment closing time becomes very user-friendly for the website and its users. The users will quickly share their comments.
hey thanks for this hack..I was wanting to do this for a long time
Wow that’s a great suggestion! ๐ I was actually looking for a way of fighting spam else than plugins (which won’t always work).. thanks for sharing info about the days left for commenting.. bookmarked so that i can implement tomorrow morning!
This customization code is in theme level so whenever we change theme we have to manually comments.
Thanks latz for this great tutorial.. I will be implementing it on my blog soon..
You can easily create a plugin using the code.
Thanks to god, the comments are not closed here. Previously I don’t knew that here comments closed after 14 days. Thanks for letting me know about this. Happy Blogging ๐