0

By default, get_comment_reply_link doesn't work for comments with a depth of 0, since the function contains the statement:

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

You can override this problem by specifying a depth greater than 0 in the $args of the get_comment_reply_link call. But if this function ignores comments with a depth of 0 by default, is there a different preferred method to get a reply link for top-level comments?

Context:

Edit

I was asked how I was getting a depth of 0.

comments.php:

if(have_comments()) {
    wp_list_comments(array(
        'status'  => 'approve',
        'walker' => new comment_walker()
    ));

functions.php:

class comment_walker extends Walker_Comment {
    function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
        $output .= var_dump($depth);
    }
}

This outputs int(0) for as many comments as I have, even if I change the parameters for start_el to $depth = 1.

Edit #2

This is is the code that's actually using get_comment_reply_link in functions.php. It works only if I modify the depth with the line marked below, otherwise it uses the depth of 0 and returns a null value:

class comment_walker extends Walker_Comment {
    function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
        $comment_id = intval($item->comment_ID);
        $post_id = intval($item->comment_post_ID);
        $max_depth = get_option('thread_comments_depth');
        $default = array(
            'depth'      => 1, // doesn't work unless I add this line to change the depth
            'max_depth'  => $max_depth
        );
        $output .= get_comment_reply_link($default,$comment_id,$post_id);
    }
}
4
  • how are you getting a comment with depth 0? Comment depth starts at 1 so a comment with no parent has a depth of 1. Depths of 0 indicate a bug in your code
    – Tom J Nowell
    Sep 25, 2022 at 17:04
  • please avoid putting blocks of code in comments, comments only support inline code. It's very likely though that you've made a mistake by setting depth to 0. Also your link to the official docs is incorrect, instead of linking to developer.wordpress.org/reference/functions/… you've linked to its code in an ancient version of WordPress
    – Tom J Nowell
    Sep 25, 2022 at 17:09
  • Okay. I'm not sure how to answer you without having to put in a block of code. In answer to your question, I'm calling a walker function. In the walker, I'm using start_el. Even if I set depth=1, if I output the depth of each comment, I get a int(0) for all top-level comments. In your linked current documentation, the function I referred to is at Line 1691-1693. Sep 25, 2022 at 17:12
  • you can edit your question using the edit link under the tags, can you also add the code that tries to use get_comment_reply_link?
    – Tom J Nowell
    Sep 25, 2022 at 17:13

1 Answer 1

0

There's no such thing as a depth 0 comment, and it should never happen, here is your problem:

    function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
        $output .= var_dump($depth);

No depth incrementing is happening so it is always 0. start_el has several responsibilities as part of the walking, and for comment loop setup.

If we look at the start_el method of the Walker_Comment class, the first thing it does is increment the depth and set the necessary global variables:

    public function start_el( &$output, $data_object, $depth = 0, $args = array(), $current_object_id = 0 ) {
        // Restores the more descriptive, specific name for use within this method.
        $comment = $data_object;

        $depth++;
        $GLOBALS['comment_depth'] = $depth;
        $GLOBALS['comment']       = $comment;

Since yours doesn't do this, $depth will never be incremented no matter how deep the walker goes, and will always remain at the default 0. You should also expect lots of PHP warnings as a result of the globals being undefined.

However, if implemented correctly, $depth will always be 1 or higher, and get_comment_reply_link will never receive a 0 value.

This and other functions need to do several other things as well to behave correctly as a comment walker. As a rule of thumb, copy paste the entirety of the method you're replacing in your custom walker, then modify it, but don't delete it entirely and start from scratch.

2
  • I might not be understanding fully, but since I'm using extends, am I not adding onto the existing functions of the defined functions, rather than overwriting them? I thought that because it seems like start_el is incrementing somehow even with the code as-is. If I add a child comment $output .= var_dump($depth);, the child comment gives me int(1), while all parent comments give me int(0). Sep 25, 2022 at 17:41
  • no, unless you explicitly call the parent method, yours replaces it fully. extend is just the keyword PHP uses for classic inheritance. This is why Widgets have a parent::__construct( call in their constructor. Even then, using parent::start_el wouldn't work well here
    – Tom J Nowell
    Sep 26, 2022 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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