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.
To accomplish this in a very easy-to-use manner, we’ll create a custom meta box inside the Post Editor itself.
Functions.php
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.
Comments.php
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
-
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.
Regards,
-
Very nice- bookmarked =)
-
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.
-
Closing comments is just a bad idea all around.
There are very few reasons to ever do it. -
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.
-
Any way to get this to work with Thesis?
-
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.
-
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.
-
great post. really like it. thanks
-
Nice tuts thanks for share . I test it in wordpress 3.0
-
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.
-
By the way, do comments have to be closed for this to work?
-
-
Hi Chris!
Sorry to ask but i’m a newbie on this, where should I insert those codes in my wordpress.org instance?
Thanks for the future responses.