WordPress 2.7 includes a lot of new enhancements, but one of the big ones is the new comment functionality. Comments can be threaded, paged, etc. This is all built in, but unfortunately, your theme must support it. So, for theme authors, I’d suggest getting to work on making your themes compatible right away.
Read on if you’re a theme author…
Actually “compatible” is not quite the right term. Old themes will continue to work fine in WordPress 2.7. It’s just the threading and paging and javascript enhancements need the theme to support it. This is much the same as the sidebar widgets, the theme has to support it for the functionality to work. So this article is really not about 2.7 compatibility, it’s about 2.7 capability.
Note that this article will explain some of the changes needed to make themes capable of supporting the new comments functions, however there’s no substitute for the real thing. Install a local copy of WordPress trunk on your home machine (possibly using XAMPP) and test it there.
Also note that this is all based on the current state of WordPress trunk, and is subject to change before WordPress 2.7 is released. However, it’s probably not going to change all that much at this point.
How to create a 2.7 compatible comments.php
2.7 Detection
If you want your theme to be backward compatible as well, then there’s a simple way to do it. Just check for the wp_list_comments function, like so:
if (function_exists('wp_list_comments')) : // new comments.php stuff else : // old comments.php stuff endif;
While you could check for the version number of WordPress, this method is better because it simply looks for the actual function you’re going to use anyway. Never make assumptions based on version number.
One of the more interesting ways I’ve seen to use this is to have the “old comments” php in a separate file entirely, which is then included. This preserves backwards compatibility for your theme in a simple way. Here’s a quick example code for that approach:
< ?php add_filter('comments_template', 'legacy_comments'); function legacy_comments($file) { if(!function_exists('wp_list_comments')) $file = TEMPLATEPATH . '/legacy.comments.php'; return $file; } ?>
Adding this code to a theme’s functions.php file will make the theme use the “legacy.comments.php” for older non-2.7 installations. That way, you can simply rename your old comments.php and then make a new one based on the new functionality. Clever.
Password Protection Check
Put this code at the top of your comments.php file. This is what lets it support the post password functionality. Note that this code is quite similar to the previous way that it was done (by checking the cookie directly), but now WordPress has a specific function to do it. You should use this function in case the functionality changes in the future, your code will be forward compatible:
if (!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME'])) die ('Please do not load this page directly. Thanks!'); if ( post_password_required() ) { echo 'This post is password protected. Enter the password to view comments.'; return; }
The Comments Loop
The Comments Loop used to look similar to this (much simplified from a real one):
if ($comments) : < ?php $comment_count = get_comment_count($post->ID); echo $comment_count['approved']; ?> Comments <ul class="commentlist"> < ?php foreach( $comments as $comment ) : // stuff to display the comment in an LI here endforeach; ?></ul> < ?php else : if ('open' == $post->comment_status) : // If comments are open, but there are no comments. else : // comments are closed endif; endif;
Basically, it went through the comments manually and output all the necessary pieces. Easy, but very manual. This also had the problem of being very inconsistent and hard to manage for your theme’s users, especially if you heavily customized it.
The new comments loop is much simpler, and much more like a normal post Loop:
if ( have_comments() ) : ?> <h4 id="comments">< ?php comments_number('No Comments', 'One Comment', '% Comments' );?></h4> <ul class="commentlist"> < ?php wp_list_comments(); ?></ul> <div class="navigation"> <div class="alignleft">< ?php previous_comments_link() ?></div> <div class="alignright">< ?php next_comments_link() ?></div> </div> < ?php else : // this is displayed if there are no comments so far ?> < ?php if ('open' == $post->comment_status) : // If comments are open, but there are no comments. else : // comments are closed endif; endif;
That new one is, in fact, a complete comments loop. No simplification at all. Unless you want something displayed for “no comments” or “comments closed”, of course.
There are three important pieces to note here:
- The have_comments() function replaces the check on the global $comments variable.
- The wp_list_comments() function now outputs all the comments. It does threading, the classes, everything new.
- There’s a new navigation section to do comment paging.
The Power of Javascript
To support the new Javascript functionality with comment threading, some minor bits of code are needed:
First, in the header.php, add this line immediately before the call to wp_head():
if ( is_singular() ) wp_enqueue_script( 'comment-reply' );
That code adds the comment-reply javascript to the single post pages, letting the comment reply links work correctly. WordPress specifically does NOT do this itself, for the reason that use of this script requires certain naming conventions and parameters in the comment form, which you’ll have to add.
So, your comment form has a new parameter that you have to add:
< ?php comment_id_fields(); ?>
This adds a bit of code to your form which makes it display two hidden inputs: comment_post_ID and comment_parent. Your form probably had the comment_post_ID before, so you need to remove it. The comment_parent is there for the javascript, so that replies to comments get threaded properly.
Also, your comment textarea MUST have an id=”comment”. The javascript expects it for focus purposes. If you used anything else, change it. Note that because of this, no other element on your page can have the “comment” ID.
Finally, the entire comment form MUST be surrounded by a DIV with an id=”respond”. In some previous themes (including the default ones), there would be an anchor tag like this:
<a id="respond"></a>
This was there to allow the link from the front page to go directly to the respond section when there were no comments already. That still happens, but now there’s a double purpose. The javascript moves the comment form to where the reply link is, so instead of it being an anchor, it needs to be a DIV that surrounds the comment form.
So, remove that anchor, and add a DIV with an id=”respond” around the entire comment form. The link from the front page still works this way with all modern browsers, and the javascript can now move the form around on the page as needed.
Next, you can replace the call to your normal “Leave a Comment” text with something like this:
<h3>< ?php comment_form_title(); ?></h3>
This makes a comment form title of “Leave a Reply” which will change to “Leave a Reply to Whoever” when somebody is replying directly to another person. You can customize this, if you like, with two parameters, like so:
< ?php comment_form_title( 'Leave a Reply', 'Leave a Reply to %s' ); ?>
The %s will be replaced with the person’s name. Note that if you don’t have the comment-reply javascript enqueued, as stated above, you won’t see the reply links for the individual comments at all.
Finally, you’ll notice that when somebody clicks “reply” and the comment form appears there, maybe they decide to cancel instead. So, that cancel link needs to be in your respond section. Here’s the code to do that, just put it right below your “leave a message” header in the comment form area:
<div id="cancel-comment-reply"> <small>< ?php cancel_comment_reply_link() ?></small></div>
That’s pretty much it for making the AJAX work. With this, the new features on the Settings->Discussion panel will work. Obviously, you can modify this somewhat as needed for your theme, these are just general principles that you’ll need to use.
Styling
Now that you have it working, there’s plenty of new styling you can add to comments. The new comments loop automatically puts every comment into an LI tag, and threads them as well, with embedded UL/LI tags. It also adds a ton of classes on all these LIs which surround every comment in this fashion:
- comment, trackback, pingback classes get added depending on the type of the comment.
- byuser gets added if the comment is by a registered user of the site.
- comment-author-authorname gets added for specific registered users.
- bypostauthor gets added if the comment is by the author of the post the comment is attached to.
- odd and even classes are added to odd and even numbered comments
- alt is added to every other comment
- thread-odd, thread-even, and thread-alt classes are the same as the odd/even/alt classes, but these only apply to the top level of each set of comments and replies
- depth-1 is added to the top level comments, depth-2 to the next level, and so on.
What’s more, a comment_class filter is provided to allow you to add your own classes. Here’s an example of that. This example function adds a microid to every comment with the microid for the comment authors given URL and email address. This sort of thing could be done in a plugin or a theme’s functions.php file, whatever.
// add a microid to all the comments function comment_add_microid($classes) { $c_email=get_comment_author_email(); $c_url=get_comment_author_url(); if (!empty($c_email) && !empty($c_url)) { $microid = 'microid-mailto+http:sha1:' . sha1(sha1('mailto:'.$c_email).sha1($c_url)); $classes[] = $microid; } return $classes; } add_filter('comment_class','comment_add_microid');
Simple and effective. It just adds the class to the given array of classes and lets the comment display functions take care of the rest.
And there you have it. It’s not hard to support the new functions. And if you need to customize your theme’s comments section even more, wp_list_comments() supports a number of parameters. Most of this is not documented yet, because WordPress 2.7 is not out until November. However, the code is relatively straightforward, and anybody with a good understanding of WordPress should be able to work it out.
Awesome as usual Otto! Of course I have to make a comment to see it in action!
I haven’t done a lot of styling yet, but you can at least see some of it here, what with the threading and my comments being a bit more blue than yours (thanks to the bypostauthor class).
I wonder how replies to replies would be handled…
Very good article, thanks for this!
[...] has written a very nice article explaining how to get a theme ready for 2.7 and the comment stuff. It’s clean, detailed and goes step by step through all the new things. [...]
Thanks, Otto. Will implement this into my site’s theme.
Wow, you guys did a lot of work, hopefully all the themers will take advantage of all these new features for comments. I’m guessing all of the code you mentioned here is build into the default theme right?
Yes, the default theme uses this new functionality and shows this stuff off fairly effectively. The old theme comment methods will still actually work, however the new functionality (thread, paging, AJAX, etc) won’t work until the theme is modified to do it this way.
Here’s the trac ticket where all these enhancements were added along with who added them: http://trac.wordpress.org/ticket/7635
Excellent advice, thanks.
Just one little nitpick: “AJAX”? Its not Ajaxified by default at all, just Javascript to move the reply form, You dont want to confuse people now do you..?
Hopefully this article will be useful to theme developers though, It can be a bit confusing at first following the default themee layouts.
Bah. I suspect that it will become more AJAXified as time goes on. I can see comment replies getting put onto the page without a refresh, for example. That would be pretty cool.
You’re a beast! Thanks for this. …back to work.
[...] WP 2.7 Comments Enhancements→ [...]
Hi Otto,
The article is great. Can you add a sample comments.php ?
A sample comment file would be especially helpful for those of us who have a theme made by someone no longer available to modify. So we have to middle through.
Also how would we deal with an existing quoting plugin and the new features?
Explain, please, for the PHP-challenged.
Peace,
Gene
The default theme has implemented this into it as of 2.7. I have not checked the classic theme, but it will likely get the same treatment.
As for how you’ll deal with the plugin, well, it really depends on the plugin. This new code makes many plugins obsolete, however the old style of comments still works fine, so if you want to keep using plugins instead of the new stuff, you can.
Quoter, no longer being updated, gives each quote a specific number, so only that item appears in the comment, even if it’s not the previous message.
I wouldn’t mind this Reply function instead, and disable that plugin, but then I’d have to find a way to do a wild card search to eliminate the extra code in the blockquotes. Do any of the search and replace plugins accomplish that?
This would be a viable solution. Our standard comments theme file is very close to the default/classic ones, with only minor changes I can, as PHP-deficient as I am fix.
I’m not averse to removing plugins that’ll no longer be needed.
But this is the lone issue I’ll need to consider.
Peace,
Gene
You could probably figure out a way to script it and convert.
If you have any way to determine which comment number the quote came from, then you can thread your comments. The comment threading works by including a comment_parent value with each comment. The contents of that column is the comment_id of the parent. So if your quoter has that, then you write a script to:
a) remove the quote
b) save the comment_id value of where the quote came from in the comment_parent
And voila, threading.
That’s above my pay grade.
As I said, I’m PHP challenged.
But if someone wants to build a Quoter liberator.
Peace,
Gene
[...] has written an excellent article on how to prepare your sites for the new Comments [...]
it is quite complicated process
This means I will have to do some work on my comment.php ? Oh, no, my God !
Thanks Otto, good insider information as always!
Thanks for the great tips.
I would like to know if with the new features, can I still separate trackbacks from comments? And also, how do I go about making my comments threaded, or is it already integrated with the new function?
Again, thanks for your help.
Will
http://will.ph/
Yes, the wp_list_comments function takes a “type” parameter to get different types of comments. It defaults to “all”, but it can also be: comment, trackback, pingback, or “pings” for both trackbacks and pingbacks combined. So wp_list_comments(’type=pings’); would give you all the pings.
Also, as you can see, the trackback/pingbacks can be integrated but styled differently. I made them yellow here for purposes of demonstration. I need to sit down and restyle my comments sections, actually.
Threading is handled automatically, and in the Settings->Discussion area, you can set a depth parameter, with how many levels deep you want the threading to go.
wp_list_comments takes several arguments, which I did not go into because most themes won’t (or should not) use them. However, it is as configurable as everything else when it comes to styling and such.
Wow. That was fast. Thanks, Otto.
Awesome. My theme could be even better than before with this power
You said: “Also note that this is all based on the current state of WordPress trunk, and is subject to change before WordPress 2.7 is released.”
My question is this. Is there a place that I can check before installing the 2.7 update to know how to bring my theme up to par?
Non-modified themes will continue to work in WordPress 2.7 (they do now anyway). They just won’t have the new threading or paging functionality.
So upgrading doesn’t mean making sure your theme is up to par in advance, you can fix up the theme after the fact.
Good to know. Thank you.
It took me a little while to test out, but it works great so far on my test site! I thought I would mention that you MUST set a thread level in the Discussion settings AND check the “Group replies” option. Otherwise it will default to 0 and you will not see threaded comments or reply links even if you have the “Group replies” option checked.
Actually, if you leave it blank (not zero), then it defaults to -1, which is unlimited levels of threading. That should probably be clarified there somehow.
Hi Otto,
I have another question. Would the function “have_comment()” accept a parameter such as type=pings so one can easily separate comments from trackbacks/pingbacks?
Thanks again for the tips.
No, it doesn’t, but that is a good idea.
Hi Otto if I can separate comments from pings I’m very happy.
Threaded comments are not my favour and if WP 2.7 comes I have less possibilities to design them,
because css doesn’t accept 6 different styles in one class.
how can I add divs to design the author or the meta section of comments?
kindly regards
Monika
I’m not entirely sure what you mean by CSS not accepting styles/classes. Styling your comments is very very easy with this code.
If you look at the code, the author data gets encapsulated inside a div with a class of comment-author. The meta data gets put inside comment-meta as well as commentmetadata. This is automatic, you don’t need to change a thing to get it.
[...] WordPress 2.7 Comments EnhancementsWP 2.7 is due out in Nov08 and has major changes to comments handling. This article from one of the core developers identifies changes to make to themesnone [...]
One thing I really think about WordPress 2.7: it is such a massive overhaul to so many aspects of WordPress (with a lot of potential for compatibility issues) that I think it should be called WordPress 3.0, with the 2.6 branch maintained for security fixes. I am actually very happy with how I’ve gotten Sachiko’s blog (I am her webmaster) running with 2.6 and the combination of plug-ins that we have, and I can see a lot of this will be broken in 2.7. That will probably be worth it in the long run, but I suspect it will be quite a while before I am able to upgrade.
This is splendid. Thank you so much.
BUT…if I had used one of the paged threaded comment plugins before, how will switching to a built-in facility of the same kind affect my old comments?
Really depends on how the old threading plugin worked. If it used comment_parent, then the threading will persist. If not, then you’ll probably lose that threading info from the old comments.
Maybe the plugin author can make a “fix” to address this issue if it occurs.
I’m really excited to see how 2.7 comes across. There have been a lot of minor issues with 2.6 that I wasn’t overly impressed with but I think if the new framework is anything to go by, this is going to be great.
I wanna try cause it’s really exciting
Great news, thanks.
Yeah!
There are some updates/changes to the comments reply functionality and template tags. Now it supports replying without JS too. Some more info: http://wpdevel.wordpress.com/2.....o-threade/
I’ve updated the post to account for the latest trunk code. I’ll try to keep it that way.
Oh my my… this is a hell lot of coding for a geek like me. Isn’t there any simple just one click solutions. Man… I want to try out these functions as well. but, what if anything goes wrong…
Correction:
Thanks a lot for this detailed guide. I couldn’t make it work by just following the steps above.
I think you need to ad the following line to the comments form after the hidden field:
comment_parent_field();
The state of the code in trunk changed since I first wrote this guide, as Ozz explains over here.
I’ve updated the guide accordingly.
Where did you update the guide? I cannot find “comment_parent_field();”
Thanks
The comment_parent_field function is gone now. It’s been replaced by comment_id_fields, which does both the parent field and the post id field.
Is there a way to change “Reply” by something else ?
Not yet, but I’ll mention it on trac so that it can get in by 2.7.
Done. In the latest trunk, you should be able to pass a “reply_text” argument to the wp_list_comments to set the text of the “Reply” link.
hi
btw, you can just try wp 2.7 on my blog: wp.blogywalkie.com. Use “test” as your password and username
this is very new, wp2.7 on my blog is prototype version. only few blogger has coppy of it,
include me. check it out guys!!!!
Have been debating whether or not to design my own theme from scratch, as I build sites everyday at work!
But after reviewing some of the new features of 2.7 think I might start build specifically for that.
Hi Otto,
Thanks for the how-to! I am excited about the new comment features. Particularly threaded comments. I was beginning to consider a plugin like JS-Kit until I heard about the new features.
Excellent work!
Byron
This is lovely, I was just struggling with making the foreach loop do a bunch of these things, and now it will come integrated. This is good!
I have a question, though. Clearly get_avatar or similar is being used inside wp_list_comments, yes? Is there (or is there going to be) a way for plugins to override that function? Because all the alternate-avatar plugins I’ve seen rely on manually inserting a function call in comments.php, and obviously that won’t be possible using wp_list_comments.
Also, do the new threaded comments come with email notification to the parent-comment author? I suppose this may be answered if/when you reply, of course!
Must be some older plugins then, because they’re doing it wrong.
get_avatar() is a pluggable function. Meaning that any plugin can override it entirely by simply making a new get_avatar() function in their own code, that takes the same parameters as get_avatar does. If they’d rather not handle all cases, it has a get_avatar filter on the result, so that a plugin can override or modify the output from the normal get_avatar function. Any avatar related plugin should use these instead of requiring the user to modify their theme manually.
Also, I don’t think notification is built in at this point, but it could probably be easily done in a plugin.
Yes, they were older; I think I’ve figured out how to modify them, though. Thanks!
One thing I’ve noticed, while going through the new comment structure, though. The lack of a class on the div that encloses each comment (the one that gets id=div-comment-xxx where xxx is the id number of the comment) is going to severely impair some css layout options. If that particular div could be given a class (comment-div or something) that we could use in conjunction with .odd, .even, etc. that would open up the options again. I realize you guys are in bug-swatting mode rather than feature mode, but, as a layout designer, I consider this a fairly significant bug.
Cheers
What, exactly, does that prevent you from doing? Because that particular DIV is easily referenced directly by simply using “ul.commentl