Loading comments in ajax – comment-reply function missing $args

I’m loading comments with AJAX.
It all works fine except reply link isn’t being rendered on the page. As I understand, the problem is can’t pass $args. How would I access $args or max_depth outside the callback?

Ajax template :

<?php
    require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );

    if (isset($_POST['id']) && $_POST['id']) {
        $comments = get_comments(array('post_id' => $_POST['id']));
        
        foreach ($comments as $comment) {            
            $GLOBALS['comment'] = $comment;
        ?>
            <div class="comment">
                <?php comment_text(); ?>
                <div class="comment__reply">
                    <?php 
                    comment_reply_link ( 
                            array_merge( 
                                    $args, 
                                    array( 
                                        'reply_text' => __( 'Répondre ', 'autourdesanimaux' ), 
                                        'depth' => $depth, 
                                        'max_depth' => get_option( 'thread_comments_depth' ) 
                                    ) 
                            ) 
                    );
                    ?>
                </div>
            </div>
        <?php }
    } ?>

This code is not recognized (I am in a custom ajax)

array_merge( 
        $args, 
        array( 
            'reply_text' => __( 'Répondre ', 'autourdesanimaux' ), 
            'depth' => $depth, 
            'max_depth' => get_option( 'thread_comments_depth' ) 
        ) 
)

Thank you

This post doesn’t explain how to access $args
Load custom formatted comment with AJAX: reply link isn’t rendered?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You do not need an existing $args variable here, you can define your own arguments and they will override the defaults. See the get_comment_reply_link() docs for the defaults.

Instead, you can pass the arguments you want directly.

    comment_reply_link( array(
        'reply_text' => __( 'Répondre ', 'autourdesanimaux' ),
        'depth'      => $depth,
        'max_depth'  => get_option( 'thread_comments_depth' )
    ) );

The $depth variable will need to be set, and it needs to be greater than zero and less than max_depth. This article describes how to get a comment’s depth by checking for the parent property on WP_Comment objects and counting.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x