Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'll explain what I am trying to accomplish.

I would like to have regular non-threaded (flat/classic) comments with a reply link to each comment. When someone replies to a comment the reply comment will end up at the top (or bottom) as the latest comment just as it would in the default non-threaded comments, but unlike the default non-threaded comment the reply should display a link to the original comment/author.

There is a proposed solution over at the wordpress forum:

http://wordpress.org/support/topic/reply-to-link-for-chronological-non-threaded-comments

I just don't get it to work. I am not sure in what files the poster is placing the different pieces of code. I tried to use the code and hook a function to comment_post:

add_action('comment_post', 'print_reply_link', 10, 2);

function print_reply_link( $comment_id, $approved ){
  if( $approved ){
    $comment = get_comment( $comment_id );
    if ( $comment->comment_parent ) {
    $parent = get_comment( $comment->comment_parent );
    $parent_link = esc_url( get_comment_link( $comment->comment_parent ) );
    printf( ' in reply to <a href="%1$s">%2$s</a>', $parent_link, $parent->comment_author );
    } 
  }
}

This returned a blank page with "in reply to authorname". After reloading the post a reply comment shows up correctly but without any link to the original comment

My coding skills are really very basic so I have no good idea how I should proceed. Would be very grateful for any help.

share|improve this question
    
What is the content of the href in the a that follows 'in reply to` as per the 'Inspect Element' tool in Chrome (or similar in your browser of choice)? –  m0r7if3r Feb 5 '12 at 12:33
    
I have solved the problem after a lot of trial and error. The trick was to get the pieces of code in the right order and places in the comments and functions files. The s in %1$s means that the argument is treated as and presented as a string. This is explained in detail in the php manual for the the sprintf function: php.net/manual/en/function.sprintf.php –  sarytash Feb 7 '12 at 23:51
3  
You should submit what you did as a solution to help keep the site organized and to help anyone who comes after you with the same issue to overcome it quickly and efficiently. –  m0r7if3r Feb 8 '12 at 0:01
    
Yes if you could post a working solution that would be great. –  Borek Mar 16 '12 at 16:23
1  
agreed, would love to see this in action. –  helgatheviking Sep 7 '12 at 23:05

1 Answer 1

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.