Non-threaded comment replies with link to original comment

This is my solution and not the proposed solution suggested on the WordPress forums. It turns out to be fairly easy, but involves a few steps.

1) Go to wp-admin->Settings->Discussion and switch off threaded comments. This will disable threading, which we want, but will also remove the ability for us to “reply to” a particular comment. We have to put that back.

2) Add the script below to your theme’s functions.php or to a plugin.

function load_script_for_fake_threading() {
    if (is_singular()) wp_enqueue_script('comment-reply');
}
add_action('wp_enqueue_scripts','load_script_for_fake_threading');

3) The printing of the comments is handled by a callback to wp_list_comments. TwentyEleven uses the function twentyeleven_comment() in its functions.php. If your theme does not have a callback you will need to create one. By default, the content of the start_el method of the Walker_Comment class in “wp-includes/comment-template.php” is used. Copy that or copy the theme’s callback and rename the function to fake_threaded_comment.

3) Find the wp_list_comments function in your theme’s comments.php and change the callback. It should look like:

wp_list_comments(array('callback'=>'fake_threaded_comment'));

4) Now we are going to cheat a little. In that callback, ‘fake_threaded_comment’, there should a call to the comment_reply_link function. We need to edit it so that we are passing hard-coded ‘depth’ and ‘max_depth’ arguments. We are setting ‘depth’ to 1 and max_depth to 2. This will get our “Reply” button/link back.

comment_reply_link( 
  array_merge( 
    $args, 
      array( 
        'reply_text' => __( 'Reply <span>&darr;</span>', 
        'themetextdomainname' ), 
        'depth' => 1, 
        'max_depth' => 2 
)));

5) Setup the link to the parent comment. In side the callback function you may have noticed references to $comment. $comment->comment_parent is the ID of the replied-to comment and get_comment_link($comment->comment_parent) gives the URL of the parent comment (not the complete anchor markup). get_comment($comment->comment_parent) will get the parent comment data. So, something like…

$pcom = get_comment($comment->comment_parent);
echo '<a href="'.get_comment_link($comment->comment_parent).'">This is a reply to '.$pcom->comment_author.'</a>';

That’s it. You can now comment on particular comments but once published the comment will show up at the end or at the beginning of the comment list depending on the load order.

Tested with TwentyEleven on WordPress 3.4.1 (slightly outdated but it is already on my dev server). I am sure there are things that themes or plugins could do to break this, but I don’t think a well behaved theme or plugin would break it.

That is my good deed for the day. Please let me know if something goes wrong.

Leave a Comment