« PHP - Displaying Gravatars In A Non-WordPress Application | WordCamp Ed - The Very First Edition »


WordPress - Legacy of comments.php For 2.7 And Beyond

2008.11.01 @ 06:30:58 -0400 under

PHP, WordPress

Disclaimer

The instructions/steps/scripts/methods given below worked for me running CentOS. It may very well work for you on other linux distributions, Red Hat-like or otherwise. Please note that if you decide to use these instructions on your machine, you are doing so entirely at your very own discretion and that neither this site, sgowtham.net, nor its author is responsible for any/all damage - intellectual or otherwise.


What Is This Feature?

During the State Of The Word address in not-so-recently held WordCamp New York, Matt Mullenweg gave a sneak preview of a plethora of new (and cool) features being included into the next WordPress release (2.7). I had since then been toying around with the almost β version (of nightly builds) for a while and now that it is in β stage, I believe I can discuss one of the really cool features and some code-change required to make some pre-2.7 themes incorporate it.

Threaded Comments / Responses. Often times, owing to the time zone difference between many readers [or my own pure procrastination], my response to a given comment would not necessarily be right below it. A commonly used practice so far had/has been to [I can't believe I am revealing this secret, but Matt did/does it too] manually update the time-stamp of my response so that it actually appears right below the appropriate comment. In some versions, this could have been done from within WordPress admin interface while in some recent versions, one needed a [graphical/textual] way of accessing MySQL database. But that’s history with version 2.7 (and hopefully beyond). For each comment that some one posts on an entry, there is now an option [from within the admin panel or in the normal post page] of replying to that comment and the response automagically shows up as a threaded comment/response in the post page :)



WordPress 2.7 Threaded Comments

Threaded Comments/Responses - Default theme in WordPress 2.7


Why Should I Change The Code?

Unless one is just getting started with blogging and uses WordPress 2.7 OR is fortunate enough to get a WordPress 2.7 ready theme, comments.php in most pre-2.7 templates does not have the capability to display threaded comments/responses.


What Is The Change?

First off, find comments.php in your WordPress theme and rename it as comments_legacy.php


cp comments.php comments_legacy.php

Next, create comments.php that is compatible with 2.7. To this effect, one can just copy the comments.php from default theme in 2.7 and edit it, if necessary. Last step is to open functions.php in your theme and add the following lines:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Hack to make sure that the pre-2.7 themes work properly in 2.7 (and beyond)
 
# Refer to WordPress Codex to learn more about add_filter()
add_filter('comments_template', 'comments_legacy');
 
# Function to decide which comment template to use, depending on
# the availability of wp_list_comments() function. If available, use
# comments.php; if not, comments_legacy.php
function comments_legacy($my_comment_file) {
  if(function_exists('wp_list_comments')) {
    $my_comment_file = 'comments.php';
  } else {
    $my_comment_file = 'comments_legacy.php';
  } 
 
  return $my_comment_file;
}

As explained in the comments to the above code, all it does is to see if a particular function, wp_list_comments() [available in 2.7], is available or not. If available, it uses comments.php; if not, it uses comments_legacy.php. When I made these changes to my β deployment, it worked like a charm and looked as follows:



WordPress 2.7 Threaded Comments

Threaded Comments/Responses - my personal theme in WordPress 2.7


divider


Your Thoughts



Opinions expressed in these pages are purely personal and do not reflect those of any other institution and/or individual
© 2002-2018 Gowtham • All Rights Reserved