comment threading




WordPress 2.7 Comment Threading Howto


The new UI of 2.7 is nice and all, but what really amazed me was that the developers integrated comment threading, at least, if the theme supports it. So after a few tweaks and going through Otto’s article a number of times until I finally understood how it works, I was able to use the new feature in my theme.

The feature has been integrated in my theme since 2.7-Beta1 was released, yet only now did I realize that my reference was a bit too technical, even for me, that I decided to write another howto that is simplified. Before jumping in, I would highly advise to disable any comment threading plugin, including Disqus, to avoid any unwanted hiccups. Of course, the decision to do this is all up to you.

Still here? Then let’s begin.

There will be two files that needs editing. Three, if you want to release your theme and make it backwards-compatible. This howto is for those that are NOT going to make their theme backwards-compatible. In other words, the instructions on this page is for WordPress 2.7 and above only.

The files that need editing would be header.php and comments.php, both can be found in your theme folder.

In header.php, you would need to add a line between the <head></head> tags, preferably at the line right before the wp_head() function.

<?php if ( is_singular() ) wp_enqueue_script( 'comment-reply' ); ?>

Simple, right? Right. In comments.php, however, it gets tricky. And a little bit dangerous. But not to worry. As long as you know how to read and compare codes, and with some patience, you’ll do just fine. But for safety’s sake, make sure you back up your comments.php file. All done? Good.

Open your comments.php file in your favorite text editor. Then, look for the line of code where you see

if ( have_comments() ) :

Below or after that, you would usually see the opening tag for a list <ul> or <ol> with a class of commentlist. To use the new threaded comments feature, anything in between the opening and closing tags of the list should be replaced with the following:

<?php wp_list_comments(); ?>

So your comments loop should look like this:

<ul class="commentlist">
<?php wp_list_comments(); ?>
</ul>

Then after that, you would need to add the following lines of code to enable paged comments, that is, if you enabled it in the Settings > Discussion page.

<div class="navigation">
<div class="alignleft"><?php previous_comments_link() ?></div>
<div class="alignright"><?php next_comments_link() ?></div>
</div>

So for the code that shows the comments, etc., the following is the resultant code:

<?php if ( have_comments() ) : ?>
<h3 class="tooltitle"><?php comments_number('No Replies', 'One Reply', '% Replies' );?> to "<?php the_title(); ?>"</h3>

<ol id="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. -->
<div class="tooltitle">No Replies to "<?php the_title(); ?>."</div>
<?php else : // comments are closed ?>
<!-- If comments are closed. -->
<div class="tooltitle">Comments are closed.</div>

<?php endif; ?>
<?php endif; ?>

Now, on to the form. The function we added in the header.php file actually makes it possible for the comment form to appear directly below the comment being replied to, but we need to make the form behave the way it is expected. The script looks for the id “respond,” so that’s what you look up in the form. Usually, it’s a place holder with an anchor tag like this: <a id="respond"></a>. It may sometimes appear in the comment form title:<h3 id="respond"><?php _e('Leave a Reply'); ?></h3>. All you have to do is remove that id and enclose the whole form in a div with the “respond” id. The form code is usually found after this line of code:

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

So that’s what you look for first before enclosing the whole comment form in a div with id="respond".

<div id="respond">

--- Comment Form Code ---

</div>

The comment form code usually starts with the comment form title of “Leave a Reply” usually enclosed in <h3></h3> (e.g. <h3>Leave a Reply</h3>)

A line of code that lets you cancel your reply must be inserted just after the comment form title.

<div id="cancel-comment-reply">
<small><?php cancel_comment_reply_link(); ?></small>
</div>

And it should be before the line of code below:

<?php if ( get_option('comment_registration') && !$user_ID ) : ?>

Still with me? Good. Just one more line of code and you should be all set. This line of code makes it display two hidden inputs: comment_post_ID and comment_parent. Your comment form must have the comment_post_ID, so you have to remove it. Simply look for this line of code:

<input type="hidden" name="comment_post_ID" value="<?php echo $id; ?>" />

and replace it with

<?php comment_id_fields(); ?>

and you’re all done. Click here to see Otto’s live sample.

Save it and overwrite your comments.php file in your theme folder and you should have a working threaded comments section. If in case it doesn’t work, you did remember to make a backup of your comments.php file, right? So all you have to do is put it back in, overwriting the one you edited, and start over again.

You can post your questions in the comments section, and I’ll see what I can do. For styling questions, I would suggest you read Otto’s article. If in case you wanted me to check up on your comments.php file before you commit the changes, you can send it over to mail at will dot ph and I’ll review it for you for free, if time permits, that is.

Happy Hunting!

8 Comments »


WordPress 2.7 Beta 1


So 2.7 is not bleeding anymore and WordPress developers have finally let the cat out of the bag. A lot of people have been testing it prior to its beta release, and most of the “complaints” are aesthetic-related. I know I’m guilty of some. I’m blaming Jane Wells for whetting my appetite for 2.7 when she posted a couple of articles showing screenshots and detailed descriptions of the new dashboard. But of course, I kid.

Now that Beta 1 is out, it is definitely time to give it a real test drive. I’m not talking about installing it on a subdomain and checking the new features out. I’m talking about real-world scenarios wherein a hundred people try to comment on one of your entries just to test the comment reply button and if it indeed threads comments, one of the new features 2.7 offers. Comment threading is theme-dependent, and it may be as simple as copying and pasting some code, but if you live to customize, it will NOT be a walk in the park.

commentform

Take for example, this site’s theme. Customizing the comment form alone gave me headaches for weeks. But I did learn a great deal when it comes to theme customizations and functions when using WP2.7’s built-in comment threading.

commentreply

The developers also beefed up a lot of the previously offered features. The Media Library is now more customizable. The Comments can be closed if an article is at a particular age. And you can control how deep threading comments will be, which has a maximum of 10 levels, for now.

mediasettings

commentsettings

The new interface, which is a far cry from 2.5, is really a hoot to work with. For me, it basically tells you what you want to know and gets out of the way for you to be more productive in producing your blog’s content. It’s more conducive to writing than before. In other words, I simply love it.

dashboard

Of course, using a beta version on a production blog is risky, and I know the risks involved. I simply had to do it for the sake of real-world testing and I want to see how it all pans out. I believe the only major hurdle the developers are trying to overcome are the aesthetics of the Dashboard, which is supposedly unfinished and needs a lot of work if you compare it to their target “look.”

Although WordPress won’t be able to release 2.7 on time (November 10th), the progress they show on an almost nightly basis is a glimpse of things to come. I surely can’t wait for 2.7 to be finally released as a stable version.

Cheers to the WordPress developers and their commitment to providing quality software.

P.S.
Comments found on the comments section of the Dashboard were from Ade, L.A., and Carlo. Please don’t sue me.

4 Comments »