How to enable reply on most-deeply-nested comments?

Here’s one suggestion using the comment_reply_link filter to change the reply links.

Instead of using a regular expression to change it, I think it’s easier to just construct the links again.

Here I use the same code as used in the get_comment_reply_link() core function, but I replace the $comment->comment_ID with $comment->comment_parent when the following condition is fulfilled:

max_depth === depth + 1

We can adjust max_depth from the backend settings or via the thread_comments_depth_max filter, where you can e.g. find an example in my answer here.

Here’s our demo plugin:

/* Regenerate the reply link but for the comment parent instead */
add_filter( 'comment_reply_link', function( $link, $args, $comment, $post )
{       
    if( 
           isset( $args['depth' ] ) 
        && isset( $args['max_depth' ] )
        && isset( $args['respond_id' ] )
        && isset( $args['add_below' ] )
        && isset( $args['reply_text'] )
        && isset( $args['reply_to_text'] )
        && (int) $args['max_depth'] === 1 + (int) $args['depth' ] 
        && ( ! get_option( 'comment_registration' ) ||  is_user_logged_in() )
    ) {         
         $onclick = sprintf( 
             'return addComment.moveForm( "%1$s-%2$s", "%2$s", "%3$s", "%4$s" )',
             $args['add_below'], $comment->comment_parent, $args['respond_id'], $post->ID
         );

         $link = sprintf( 
             "<a rel="nofollow" class="comment-reply-link" 
                 href="https://wordpress.stackexchange.com/questions/235356/%s" onclick="https://wordpress.stackexchange.com/questions/235356/%s" aria-label="https://wordpress.stackexchange.com/questions/235356/%s">%s</a>",
            esc_url( 
                add_query_arg( 
                    'replytocom', 
                     $comment->comment_parent, 
                     get_permalink( $post->ID ) 
                ) 
            ) . "#" . $args['respond_id'],
            $onclick,
            esc_attr( 
                sprintf( $args['reply_to_text'], $comment->comment_author )
            ),
            $args['reply_text']
        );
    }
    return $link;
}, 10, 4 );

Hope it helps.

Leave a Comment