1

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?

1
  • 1
    You shouldn't be loading WP in a standalone PHP file, it's extreme bad practice, it's insecure, fragile, and has lots of other problems. If you need to make AJAX calls use the REST API to handle them, or at least the old admin AJAX API
    – Tom J Nowell
    Commented Apr 3, 2021 at 20:19

1 Answer 1

0

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.

1
  • Thank you Andrew ! It works with your code and the solution you linked for the depth :-)
    – aroh
    Commented Apr 3, 2021 at 19:40

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.