Clicking Comment "Reply" Button only replies to first comment

I'm editing my comments.php, and within it I've added a reply button like this:

div class=reply
      a rel=nofollow class=comment-reply-link 
        href=?php comment_reply_link(array('reply_text' = 'Reply this comment'), comment_ID()); ? data-commentid=3 
          data-postid=487 data-belowelement=div-comment-3 data-respondelement=respond 
          data-replyto=Reply to admin aria-label=Reply to admin Reply
      /a
/div

When I click on the reply button under any comment, the comment form pops up beneath the first comment, and only replies to the first one, rather than the one I clicked on. Can anyone explain the errors in this code and describe how to fix it?

I also tried doing

div class=reply
  ?php comment_reply_link(); ?
/div

But the comment_reply_link() returns null, I believe.

I list the comments like so:

?php
if (have_comments()) : ?
    ol class=post-comments
        ?php
        wp_list_comments(array(
            'style'       = 'ol',
            'short_ping'  = true,
            'callback' = 'better_comments'
        ));
        ?
    /ol
    ?php
endif;

and have a function better_comments() {...} which includes the above HTML for the reply div.

Here's the first bit of that function:

li class=comment byuser comment-author-admin bypostauthor even thread-even depth-1 id=comment-3 style=margin-bottom: 0.7em;
    div id=div-comment-3 class=comment-body
        div class=comment-author vcard
            a class=comment-author-text
                b?php echo ucwords(get_comment_author()) ?/b
            /a

Thanks.

Topic comment-form comments Wordpress

Category Web


Your comment replies do not work because you're using a custom comment rendering callback function named better_comments, and this function hardcodes the classes IDs and other attributes rather than calling functions such as comment_ID.

I strongly suggest abandoning this function, and instead opting for the default commenting from a default theme. This way you can start from a known good solution, and add the functionality you want correctly and incrementally.

Additionally, lots of the things you want to add do not need a custom comment renderer, e.g. changing the reply link text can be done with filters, as can adding extra links.

You might also have forgotten to enqueue the comment reply script:

    // Threaded comment reply styles.
    if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
        wp_enqueue_script( 'comment-reply' );
    }

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.