how to get the comment ID in the front end when the REPLY button is clicked?

I think I might be able to parse the URL for the replytocom parameter and get the comment ID from that. However, it would mean that my plugin would not be fully compatible with other plugins that can remove this parameter (most notably Yoast SEO). Plus, it feels a bit hacky. Is there another way?

Endgoal: I need that comment ID to use in an AJAX request – to fetch data (such as the comment thread depth) so that I can display custom data in the front end.

2 Answers
2

Here are few ideas:

  • It’s possible to inject custom HTML or data attributes via the comment_reply_link filter, but I think it would be better to keep it unobtrusive.

  • One could try to get the comment parent value from the reply comment form:

    <input type="hidden" name="comment_parent" id='comment_parent' value="0" />
    

    after it has been updated with the corresponding value.

  • Another approach would be to scan for IDs in the HTML of the comments list:

    Generated by the Walker_Comment::html5_comment():

    <li id="comment-34" class="comment even thread-even depth-1">
        <article id="div-comment-34" class="comment-body">
        ... cut...
    

    Generated by the Walker_Comment::comment():

    <li class="comment even thread-even depth-1 parent" id="comment-34">
        <div id="div-comment-34" class="comment-body">
        ... cut...
    

    One could e.g. search for both cases, but it’s possible for the user to override these with a custom output.

    Also note the depth information here.

  • One could try to parse the onclick attribute of the replyto link:

    onclick='return addComment.moveForm( "div-comment-34", "34", "respond", "123" )'
    

Leave a Comment