Inform user about automatic comment closing time

To prevent spammers from flooding old articles with useless comments you can set WordPress to close comments after a certain number of days:
close-comments
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.

close comment example

Comments are closed.

14 comments

  1. Piet

    Very useful snippet, thanks for sharing!
    I added it immediately to my Code Snippets library

  2. Lois G. Richards

    Wow, these are awesome! I am following this rules on my site for old articles. It really helped me.... thank you latz.

  3. Jurij

    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.

  4. Startup

    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

  5. Angel Garcia

    Very useful info.

    Off-topic... Can I use Xtreme One Framework for commercial purposes?

  6. Johnson

    Closing comment is not solution in community site to stop spam. I think nofollow tag is only solution to stop spamming.

  7. Alex

    I have idea for good post 🙂 Write article about creating ajax comment submit with/without a plugin

  8. Indu jain

    Informing users about the comment closing time becomes very user-friendly for the website and its users. The users will quickly share their comments.

  9. chirag

    hey thanks for this hack..I was wanting to do this for a long time

  10. Eugenio (SEO)

    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!

  11. Lexi

    This customization code is in theme level so whenever we change theme we have to manually comments.

  12. Ahmad Raza

    Thanks latz for this great tutorial.. I will be implementing it on my blog soon..

  13. Latz

    You can easily create a plugin using the code.

  14. Chetan Gupta

    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 😀