Like the blog? Get the book »

Custom Message After the Comments

Custom Message After the Comments

Have you ever wanted to close a comment thread, but leave a note to communicate why the thread is closed? Many blogs will just update the content of the blog post to say that comments are closed and why. That’s better than nothing, but that puts the message in a bit of an awkward place.

The ideal place for that custom messaging is after the comment thread, where the comment form would normally be. After all, that’s where it makes the most sense.

Real-world example

One example situation is the giveaway contest we just ran. Like many blog-based comments, it was comment-to-win. That means it’s best to close the comments when the contest is over, so people clearly understand that it’s over.

We do close comments after a few months on this site to avoid spam and questions that come in way after the fact and will probably never be answered, but this is a case where we close comments much sooner than a few months. When we closed the contest, we actually put the winners of it as a custom message beneath the comment thread.

Screenshot: Custom message after the comments

To accomplish this in a very easy-to-use manner, we’ll create a custom meta box inside the Post Editor itself.


We need to hook into two actions for this. We’ll use the admin_menu and add_meta_box to create the meta box. Then we’ll tap into save_post and use update_post_meta to actually save the value entered.

// After Comments Message

add_action('admin_menu', 'custom_comments_message_hooks');
add_action('save_post', 'save_custom_comments_message');

function custom_comments_message_hooks() {
	add_meta_box('custom_comments_message', 'Custom Comments Message', 'custom_comments_message_input', 'post', 'normal', 'high');
function custom_comments_message_input() {
	global $post;
	echo '
<input id="custom_comments_message_noncename" name="custom_comments_message_noncename" type="hidden" value="'.wp_create_nonce('custom_comments_message').'" />';
	echo '<textarea id="custom_comments_message" style="width: 100%;" cols="30" rows="5" name="custom_comments_message">'.get_post_meta($post->ID,'_custom_comments_message',true).'</textarea>';
function save_custom_comments_message($post_id) {
	if (!wp_verify_nonce($_POST['custom_comments_message_noncename'], 'custom_comments_message')) return $post_id;
	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
	$custom_comments_message = $_POST['custom_comments_message'];
	update_post_meta($post_id, '_custom_comments_message', $custom_comments_message);

That is the entire UI for creating this functional data-saving meta box. Now we just need to actually use that value.


There is some logic in the comments.php file which tests if comments are open or not. If they are, the comment form is output, if they are not, some kind of “comments closed” message is output. We’ll be tinkering around in the “if they are not” section (the “else” code below) and adding a check for our special custom field value. If it’s present, output that, if not, output the default.

<?php if ( comments_open() ) :

  // regular outputting of comments stuff in here

<?php else: 

	global $post;

	$custom_message = get_post_meta($post->ID, '_custom_comments_message', true);
	if ($custom_message != '') {
		echo '<div id="after-comments-message">';
		echo $custom_message;
		echo "</div>";
	} else {
		echo '<div id="after-comments-message">Comments are closed. If you have something really important to add, <a href="/contact/">contact us</a>. Thank you!</div>';
	} ?>
<?php endif; ?>

That’s all there is to it! Now you have a fancy custom box right in your Post Editor where, should you choose, you can close comments and write a custom message to display after the existing comments.

15 responses

  1. Melvin Ramos | Self Help 360

    Hi Chris,

    I saw a message at another blog when the comments was closed and was wondering how to do it. Now I know thanks to you.


  2. Very nice- bookmarked =)

  3. Nathan Bestwick

    We’re very proud of our commenting design, let us know what you think, but I think this would be the icing on the cake – thanks.

  4. Closing comments is just a bad idea all around.
    There are very few reasons to ever do it.

    • Our holiday contest was one perfect example. Otherwise, I close comments after a few months on all my sites. Conversation slows down to almost nothing on old posts, yet spam input from them goes up and up. If you do leave them open, ad someone comments, it’s common for them to get no response especially since my own desire as an author to engage with new comments on old posts is pretty low. I like to make sure people have an avenue to communicate if they need to regarding specific posts so we do this (404 skitch link removed).

      If you have other theories, by all means, share.

      • Yeah Chris, you make some good points. It’s just a pet peeve of mine to find an engaging discussion that I can contribute to, only to find the discussion closed.

        The web is so amazing to me when I find some obscure five-year-old discussion about say, the carburetor on a vintage Fiat. I learn from others, then contribute my own rebuild experiences so that another can learn years down the road.

        I suppose for a timely tech-oriented site like css-tricks, it makes sense to close it down. But still it feels a bit like “letting the spammers win.”

        I’m dreaming to think one day we’ll have perfect spam protection, but I certainly hope we do. (Akismet is pretty darn good, but not perfect.) Spammers, and the propensity to close old posts for protection, diminishes the full potential of knowledge communities.

        If I ever run a site as popular as yours, I may see things differently. ;)

  5. In the function save_custom_comments_message($post_id) I’d add the following under the nonce confirmation to ensure the user has editing rights and $post is a post.

    if ('post' != $_POST['post_type'])) { return; }
    if (!current_user_can('edit_page', $post_id)) { return $post_id; }

    Have used this code several times and can’t remember the source, I’m afraid.

  6. Mitchell Wischmann

    Any way to get this to work with Thesis?

  7. I guess it makes sense to close some old comment threads (holidays topics, etc)…but not all. I wanted to leave a comment on another post (March 2010), ironically I found this recent post.

  8. After giving it some more thought….

    Perhaps an alternative would be to *turn on moderation* after a set period. That’s something I might try first.

  9. great post. really like it. thanks

  10. news wordpress

    Nice tuts thanks for share . I test it in wordpress 3.0

  11. I’ve tried and can’t get it to work. Having problems trying to figure out where exactly to put the code in the comments.php file. Can you help.

  12. Hi Chris!

    Sorry to ask but i’m a newbie on this, where should I insert those codes in my instance?

    Thanks for the future responses.

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