Last week I explained about two of the three enhancements available in WordPress 2.7. This week it’s all about the new comment options.
Before I continue however, there seemed to be some confusion last week from a comment or two. These changes are enhancements. Your existing themes will still work with WordPress 2.7, you will just not get the benefits or be able to use some of the new features until these changes are made. So don’t suddenly think your site is going to break or stop working when you upgrade to 2.7!
In the admin for 2.7 there are 3 new comment options:
The first new feature doesn’t require any theme changes, however if you want to use either of the second two features then we need to be updating the comments.php theme file.
The WordPress team have simplified the comment code a lot now, so that it’s very similar to how the loop works for posts. They’ve also added in a lot of auto features such as various classes to help you target the post author, alternating comments etc. So no more need to hack code around to do this yourself (or no more need for the author highlight plugin if you had it).
If you have already got a copy of the latest beta (version 3 at the time of writing), then open up comments.php under the default theme directory. If you haven’t got this then you can download just this file – default comments.php File
The first piece of new code in the file is
< ?php
if ( post_password_required() ) { ?>
<p class="nocomments">This post is password protected. Enter the password to view comments.</p>
< ?php
return;
}
?>
This check is already in place in the old default comments.php file. As you can probably tell, it checks if the post is password protected and if it is, it requests the password.This has simplified the original version and is a lot more easier to read. It also means it’s more future proof so it’s worthwhile changing if you password protect your posts at all.
This is the point at which comments are checked for, and if they are it lists them out.
< ?php if ( have_comments() ) : ?>
<h3 id="comments">< ?php comments_number('No Responses', 'One Response', '% Responses' );?> to “< ?php the_title(); ?>”</h3>
<ol class="commentlist">
< ?php wp_list_comments(); ?>
</ol>
<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. -->
< ?php else : // comments are closed ?>
<!-- If comments are closed. -->
<p class="nocomments">Comments are closed.</p>
< ?php endif; ?>
< ?php endif; ?>
This is where a great improvement has been made, by making the code similar to the loop code. Going through this line by line we have
For point 2 I’ll also point out that the first parameter of the function comments_number() is ‘No Responses’ which is a bit of a waste in this code, as this will never see the light of day because this heading is only displayed if there are comments. Personally I would move the header above the first if statement, however I’ll offer up my own sample code later on.
The output for each comment now looks like the following HTML code
<li class="comment byuser comment-author-sarah bypostauthor odd alt thread-odd thread-alt depth-1" id="comment-999"> <div id="div-comment-999"> <div class="comment-author vcard"> <img alt='' src='http://www.gravatar.com/avatar/img.jpg' class='avatar avatar-32' height='32' width='32' /> <cite>Sarah</cite> Says: </div> <div class="comment-meta commentmetadata"><a href="http://wordpress-address.com/?p=99&cpage=1#comment-999">November 30th, 2008 at 10:00 am</a></div> <p>Comment content goes here.</p> <div class="reply"></div> </div> </li>
As you can see, we have a number of new CSS classes available to us without the need for plugins or theme hacking. In the class list for the list item we have
To then use these classes to your benefit you can add some new styles in to your CSS stylesheet, after the main comment styles (else they will overwrite your custom settings). I highly recommend highlighting the author’s comments on the post, and possibly having some sort of distinction between alternating comments. For a multi author blog the site owner could then have his own comments highlighted even more distinctly, and perhaps other site authors could have a different distinction too. So for example take this scenario
If you add the relevant classes in that order then each item will override the previous items.
Next week will be the final part in this enhancement series, where I’ll explain how to use the new threaded comments and also explain how to control your comment output a little more than the default settings.
Author comments are in a darker gray color for you to easily identify the posts author in the comments
Comments are closed since this post is older than 30 days. However, you can continue this discussion in our popular Blogging Forums
Thanks for the heads up. I am sure, I need to do a lot more to adapt my theme to 2.7… I am still sitting on 2.5 as I thought, I would do a one-time switch to 2.7.
These platform upgrades happen too frequently that people are forced to spend a lot of time on other things than blogging
Cheers, Ajith
Hi Ajith, the changes between 2.5 and 2.7 are minimal. They may sound a lot but I’ve done them on my own theme and it took me around 30 minutes, mainly because I wanted to split up my comments and pingbacks (which I’ll be explaining how to do next week).
As I mentioned, you only need to add in the changes to the comment code if you want to make use of the new features. If you don’t then you don’t need to change your theme. It will continue to work regardless.
To be fair, I’d rather WordPress continually be improved offering the option of a variety of new features. You don’t have to use them, but they’re there if you want them. Much better than a stagnant blogging platform.
Very informative posting Sarah!
Thank you for the nice heads up. I found your site very interesting and full of cool stuff that I might use. Keep up the good work.
Hi Sarah,
Just a question. When did post_password_required get introduced? Was it in 2.7?
If so, wouldn’t the theme need to be backward compatible as well?
Ajay, yes post_password_required() is a new 2.7 tag/function. Everything I’ve written about has been introduced in 2.7.
I’m not sure what you mean be backward compatible. If you upgrade to 2.7 then you can use these features. If you release themes and want to have them work for both 2.6 and 2.7 then you will need to check the version number or check if the function exists first eg.
if (function_exists(post_password_required) :Thanks for clarifying.
I was talking about this on a theme developers point of view.
Thank you!
Thanks for the article. Very helpful. I am curious about one thing – I have set up my depth and post-author-admin styles but there seems to be a conflict. If the admin is replying (a depth comment), I want it to override the depth styles. Unfortunately, when it does override, the depth styles do not work at all and when they do work, the admin style does not. Any ideas?
Hi Cole, it sounds like your styles need fixing in the right order. If you post up about this in the discussion forums – http://www.bloggingtips.com/forums/ and post up your two styles then we can look at them and work out how to make them work together. However as a quick point, the post-author-admin style will need to come after the depth style. My solution would be to specify the depth style to both depth and the post-author-admin ie.
depthstylename, .post-author-admin {// depth styles
}
.post-author-admin {
// additional admin styles
}
this way the post-author-admin style will inherit the depth styles and then just change what it needs to.
However if that doesn’t work or doesn’t make sense, post up in the forum and we can help you there