• I am struggling, trying to create comment list template, but my code is not displaying unapproved comment preview after comment being submitted.
    The url has hash after comment being submitted, but the there is no preview: ?unapproved=55&moderation-hash=2e64a1c266a37ac84db938d44cae7644#comment-55

    here is my code

    <?php
    // Recursive function to display nested comments
    function display_comments($comments, $parent_id = 0) {
        foreach ($comments as $comment) {
            if ($comment->comment_parent == $parent_id) {
                ?>
                <li id="comment-<?php echo $comment->comment_ID; ?>" <?php comment_class(); ?>>
                    <article id="div-comment-<?php echo $comment->comment_ID; ?>" class="comment-body">
                        <figure class="comment-author vcard">
                            <?php echo get_avatar($comment, 100); ?>
                        </figure>
                        <div class="comment-box">
                            <footer class="comment-meta">
                                <div class="comment-meta-data">
                                    <span class="comment-author">
                                        <?php echo get_comment_author_link($comment); ?>
                                    </span>
                                    
                                    <span class="comment-date">
                                        <a href="<?php echo esc_url(get_comment_link($comment)); ?>">
                                            <time datetime="<?php echo get_comment_date('c', $comment); ?>">
                                                <?php
                                                $comment_time = strtotime($comment->comment_date);
                                                $comment_date = get_comment_date('', $comment);
                                                $comment_time_formatted = date('H:i', $comment_time);
                                                echo $comment_date . ' at ' . $comment_time_formatted;
                                                ?>
                                            </time>
                                        </a>
                                    </span>
                                </div>
                                <span class="edit-reply">
                                    <span class="edit-reply">
                                    <span class="edit-link">
                                        <?php
                                        $edit_comment_link = get_edit_comment_link($comment->comment_ID);
                                        if ($edit_comment_link) {
                                            echo '<a href="' . esc_url($edit_comment_link) . '">' . __('edit', 'ghiyats') . '</a>';
                                        }
                                        ?>
                                    </span>
                                    <div class="reply">
                                        <?php
                                        $reply_link = esc_url(add_query_arg('replytocom', $comment->comment_ID, get_permalink()) . '#respond');
                                        ?>
                                        <a rel="nofollow" class="comment-reply-link" href="<?php echo $reply_link; ?>" data-commentid="<?php echo $comment->comment_ID; ?>" data-postid="<?php echo get_the_ID(); ?>" data-belowelement="div-comment-<?php echo $comment->comment_ID; ?>" data-respondelement="respond" data-replyto="Balasan untuk <?php echo get_comment_author($comment); ?>" aria-label="Balasan untuk <?php echo get_comment_author($comment); ?>"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-reply-all-fill" viewBox="0 0 16 16">
      <path d="M8.021 11.9 3.453 8.62a.719.719 0 0 1 0-1.238L8.021 4.1a.716.716 0 0 1 1.079.619V6c1.5 0 6 0 7 8-2.5-4.5-7-4-7-4v1.281c0 .56-.606.898-1.079.62z"/>
      <path d="M5.232 4.293a.5.5 0 0 1-.106.7L1.114 7.945a.5.5 0 0 1-.042.028.147.147 0 0 0 0 .252.503.503 0 0 1 .042.028l4.012 2.954a.5.5 0 1 1-.593.805L.539 9.073a1.147 1.147 0 0 1 0-1.946l3.994-2.94a.5.5 0 0 1 .699.106z"/>
    </svg></a>
                                    </div>
                                </span>
                                </span>
                            </footer>
                            <div class="comment-content">
                                <?php comment_text($comment); ?>
                            </div><!-- .comment-content -->
                            <?php if ($parent_id != 0) { ?>
                                        <span class="reply-to">
                                            <?php echo 'Reply to ' . get_comment_author($parent_id); ?>
                                        </span>
                                    <?php } ?>
                        </div>
                    </article>
                    <?php
                    // Check if the comment has any children
                    $children = get_comments(array(
                        'parent' => $comment->comment_ID,
                        'status' => 'approve',
                        'order' => 'DESC'
                    ));
                    if ($children) {
                        echo '<ol class="children">';
                        display_comments($children, $comment->comment_ID);
                        echo '</ol>';
                    }
                    ?>
                </li><!-- #comment-## -->
                <?php
            }
        }
    }
    
    // Get the comments for a specific post or page
    $comments = get_comments(array(
        'post_id' => get_the_ID(),
        'status' => 'approve', // Only approved comments
        'order' => 'DESC' // Order by oldest first
    ));
    
    if ($comments) {
        $comments_count = wp_count_comments(get_the_ID());
        $comments_title = sprintf(
            _n('%1$s comment on "%2$s"', '%1$s comments on "%2$s"', 'ghiyats', $comments_count->approved),
            number_format_i18n($comments_count->approved),
            get_the_title()
        );
        ?>
        <div id="_comments-<?php echo get_the_ID(); ?>" class="oxy-comments">
            <h3 class="comments-title">
                <?php echo esc_html($comments_title); ?>
            </h3>
            <ol class="comments-list">
                <?php
                display_comments($comments);
                ?>
            </ol>
            <div class="comments-navigation">
                <div class="previous-comments-link"><?php previous_comments_link(); ?></div>
                <div class="next-comments-link"><?php next_comments_link(); ?></div>
            </div>
        </div>
        <?php
    } else {
        echo 'No comments found.';
    }
    ?>
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    When you get comments, you are only requesting for approved comments. The preview comment will not be within the returned comments. You either: need to get all comments, approved or not, and when you display the comments, skip over any unapproved comments besides the one passed in the URL query string;
    or, query for the comment to preview separately and display it prior to your usual list of comments.

    Either way, confirm the hash is valid to ensure only the user posting the comment can see the preview.

Viewing 1 replies (of 1 total)
  • The topic ‘Display unapproved comment preview’ is closed to new replies.