PureNews

PureNews is an amazingly sleek and powerful news theme with unlimited color variations.

View full feature list Check out the live demo Buy this theme today

WordPress 2.7 Theme Enhancements II

Posted by on 30th Nov 2008 WordPress Coding & Design 17 comments

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!

New Comment Options

In the admin for 2.7 there are 3 new comment options:

  1. Automatically close comments on posts older than X days
  2. Enable threaded (nested) comments
  3. Break comments into pages

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.

New Comment Code and Comment Pagination

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

Password Protection Check

The first piece of new code in the file is

[sourcecode language="php"]< ?php
if ( post_password_required() ) { ?>

This post is password protected. Enter the password to view comments.

< ?php
return;
}
?>[/sourcecode]

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.

Comment Output

This is the point at which comments are checked for, and if they are it lists them out.

[sourcecode language="php"]< ?php if ( have_comments() ) : ?>

< ?php comments_number('No Responses', 'One Response', '% Responses' );?> to “< ?php the_title(); ?>”

    < ?php wp_list_comments(); ?>

< ?php else : // this is displayed if there are no comments so far ?>

< ?php if ('open' == $post->comment_status) : ?>

< ?php else : // comments are closed ?>

Comments are closed.

< ?php endif; ?>
< ?php endif; ?>[/sourcecode]

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

  1. If there are comments on the post…
  2. Echo our a h3 heading with how many comments there are
  3. Then uses the new wp_list_comments() template tag to list out the comments within an ordered list
  4. We then get the previous and next comments links which are there for the pagination feature should you choose to enable it.
  5. Else, if there are no comments…
  6. Check if comments can be made
  7. Else state comments are closed

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

[sourcecode language="html"]


  • Sarah Says:

    Comment content goes here.

  • [/sourcecode]

    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

    comment
    Standard comment class available on every comment. If the ‘comment’ is actually a pingback or trackback you’ll have the class ‘pingback’ or ‘trackback’ respectively
    byuser
    This class is added if the comment is made by someone who is a registered user of (and logged into) the site.
    comment-author-sarah
    This is added for commentators who are registered users of the site. Of course ‘sarah’ is changed to the user’s nickname
    bypostauthor
    Added if the commentator is the post author – aka ‘Author Highlight’
    odd
    This, or ‘even’ is added for odd and even posts
    alt
    This is added to each alternating comment
    thread-odd
    This is the same as the odd class (and there is a thread-even), however it’s only added to the top level comments – only of use when using threaded comments
    thread-alt
    The same as the alt class however, as above, is only added to top level comments, which is only of use/interest when using threaded comments
    depth-1
    The class depth-X denotes which depth the comment is at, so depth-1 is the top level, depth-2 are replies to top level comments etc.

    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

    • Comments have alternating background colours – use ‘alt’
    • Registered users have a different background colour to others – use ‘byuser’
    • The site owner has a different background to everyone else – use ‘comment-author-nickname’ where the nickname is the nickname of the site owner
    • Post authors get their comment highlighted – use ‘bypostauthor’

    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.

    Other posts in this series

    1. Your Theme and WordPress 2.7
    2. WordPress 2.7 Theme Enhancements III

    A PHP Developer using WordPress to power both blogging and commercial CMS sites. I've written and released a couple of plugins for WordPress and am currently writing plugins for use on commercial websites.

    17 comments - Leave a reply
    • Posted by Ajith Edassery on 30th Nov 2008

      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

    • Posted by Sarah on 30th Nov 2008

      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.

    • Posted by rustin on 1st Dec 2008

      Very informative posting Sarah!

    • Posted by June@Baby Gates on 1st Dec 2008

      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.

    • Posted by Ajay on 10th Dec 2008

      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?

    • Posted by Sarah on 11th Dec 2008

      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.

      <code>if (function_exists(post_password_required) :</code>

    • Posted by Ajay on 11th Dec 2008

      Thanks for clarifying.

      I was talking about this on a theme developers point of view.

    • Posted by Jack on 13th Dec 2008

      Thank you!

    • Posted by Cole on 26th Dec 2008

      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?

    • Posted by Sarah on 2nd Jan 2009

      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.

      <code>depthstylename, .post-author-admin {
      // depth styles
      }
      .post-author-admin {
      // additional admin styles
      }</code>

      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 :)

    • Posted by storkclub on 7th Oct 2010

      Hello Sarah,.thanks for posting this,i do love the new comment feature coz u can customize it liek that one Automatically close comments on posts older than X days..u just have to set it and not worry about closing the comment manually.