admin管理员组

文章数量:1296503

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?

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?

Share Improve this question asked Apr 3, 2021 at 8:09 aroharoh 132 bronze badges 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
Add a comment  | 

1 Answer 1

Reset to default 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.

本文标签: Loading comments in ajaxcommentreply function missing args