How to change comment threading and ‘in-reply-to’ in WordPress to match mine

In response to some database performance problems I was noticing with the high-traffic, never-dying astrology debunking post, threaded comments are now disabled so as to force the blog to honor the 50-comment-per-page limit. I figured I had best nip this in the bud before HostPapa decided to kick my ass.

WordPress’ default behaviour involves only rolling over to the next page of comments, after 50 top-level comments. This is by design, I guess. While I could have just dropped that limit to 25, or hacked out the part where it looked only for top-level posts (thus breaking nesting when it crossed a page threshold), I decided to do things the hard way, since I had decided to eliminate threading altogether anyway. Gear-headed stuff below the fold.

Here’s what I did, if you’re using WordPress and like this setup. It’s all cosmetic, and none of it affects any of the data behind your website in the database proper.

In the blog theme that you’re using, find your comments override function, whatever that happens to be. It will probably be in the theme’s functions.php, but consult your theme’s docs to figure it out. Pick a spot in the comment header or footer that you like (you’ll obviously need to know a bit about HTML for this), and add:

<?php if ($comment->comment_parent != '0') : ?>
<br />
<a href="<?php $commentparent = get_comment($comment->comment_parent); $commentparentpage = get_page_of_comment($commentparent) ; echo htmlspecialchars( get_comment_link( $commentparent, array('page' => $commentparentpage) ) ) ?>">in reply to <?php echo $commentparent->comment_author; ?></a>
<?php endif; ?>

You can omit the <br /> if you don’t want it to linebreak before adding the “in reply to” link.

In WordPress’ comment-template.php, in the get_comment_reply_link function, find these lines:

if ( 0 == $args['depth'] || $args['max_depth'] <= $args['depth'] )

And add // at the start of them, thus turning the lines into comments and ignored.  This will disable checking for whether an individual comment should have the “reply” link in it at all, as normally it depends on whether or not you have threaded comments.  I’d prefer to allow people to continue using the function, even if it works differently now.

In WordPress’ comment.php, find this line in the get_page_of_comment function:

// Count comments older than this one

The line immediately following that, there’s an SQL statement.  Find the part that says:

AND comment_parent = 0

Delete that part.  This will ensure that when counting up the number of comments in a post, it will not only search for top-level comments, but will also count “child” comments which are created when someone replies to something.

You should now have the “In reply to” function working as it does here, whether your comments are set to threaded or not.  It should also correctly account for the page that the replied-to comment is on.  Unfortunately this will break if you ever update either your theme or WordPress, but with these instructions you should be able to hack it back in pretty quickly.

Performance-wise, you’ll find that having comments not threaded will drastically reduce the amount of PHP processing that has to be done in order to render the comments in the correct order.  There’s double the number of calls to get_page_of_comment in each page view, which means it will have to poll the database for the number of previous comments in the thread on every single comment, but it was already doing it once, and you’ll find with far less than half the number of comments visible at a time, and people mostly interested in only the last page of comments, you’ll make back your performance in spades.  You’ll also find that since each page contains fewer comments, they load quicker, and the database will get less overtaxed by frequent refreshing of the page  — though, seriously, if you’re that interested, just subscribe to it when you comment.  I promise your e-mail won’t get spammed.  Well, unless the post itself gets spammed, though Akismet is a pretty good spam filter.  An added bonus of using the same e-mail address every time is, if you have a Gravatar registered with that e-mail address, that will show up as your avatar icon.

And if any of you gearheads wants to point out that there’s a better way of doing this, e.g. by building it as a plugin, you’d better damn well be prepared to provide me with the support I’d need to make the leap into plugin programming in my already-strapped time.  If you’d like to do it yourself, just make it GPL 3, and post a link to this post as where you got the idea.  Drop me a line and I’ll install it, and pimp it for you.

If you’d like to go a step further, and add comment numbering to the comments, I’ll put together another post sometime.  It’s another hacking of the WordPress core files and theme, but it requires only one more database call per page view.

How to change comment threading and ‘in-reply-to’ in WordPress to match mine
The Orbit is still fighting a SLAPP suit! Help defend freedom of speech, click here to find out more and donate!

2 thoughts on “How to change comment threading and ‘in-reply-to’ in WordPress to match mine

  1. 1

    I really like the new look for the comments. My brother in law just lent me his HTML and XHTML textbook from when he took his college course. When I am done digesting it, I may decide to get a bit adventurous with my blog.
    BTW- I just wrote up a 4000+ word criticism of James claims about rectification and Polaris software, I hope you take the time to read it. You and Stephanie will likely be the only people willing to sit through it. I imagine I’ll get a tl;dr message from James. So if you guys don’t read it then I spent like 5 hours for naught.
    No guilt trip there.

  2. 2


    How can I display “in reply to [author of parent comment]” in my threaded comments section?

    It’s just the one bit of code I’d like to implement on my blog.


Comments are closed.