2

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

0

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" )'
    
6
  • Thanks! I tried your final option (parsing onclick) and it worked. I chose it because it's the easiest for me to implement. I'll post the code. However, I'm using AJAX to get the parent ID whilst your other suggestions tell me that I can get the parent ID directly so I might try your 2nd suggestion too.
    – woodduck
    Commented Jul 30, 2016 at 8:50
  • Great, thanks for sharing your implementation. Regarding suggestion #4, it might be more stable to parse out the second input parameter of addComment.moveForm(), as the div-comment- part of the first input parameter can change. Regarding suggestion #2, this is the same ID as in e.g. #4. So are you looking for the grandparent ID? @woodduck
    – birgire
    Commented Jul 30, 2016 at 9:40
  • Not sure what you mean by the IDs in suggestion #1 and #'4 being the same. In your examples, they are 0 and 34, respectively. I need just the parent. EDIT: Are you referring to the 'respond' value (123 in your example)? I'm not sure what that is, but can't be the parent - wouldn't the parent ID be < 34?
    – woodduck
    Commented Jul 30, 2016 at 10:53
  • When we press the "Reply" link, then the hidden comment_parent input is assigned the value of 34 by javascript, the same ID value as in #4. I was then just wondering what you meant by "However, I'm using AJAX to get the parent ID", I guess you want to fetch the parent for the comment with ID=34. (grandparent of the comment we're about to write) No didn't meant the respond value, that's the current post ID @woodduck
    – birgire
    Commented Jul 30, 2016 at 11:00
  • Ah, I see. Thanks for the clarification. Yes, I want the grandparent then. For a while I thought that WP puts this ID in the hidden field attributes, so that I could parse that directly instead of using AJAX to look it up. But it's fine as it is. gets the job done.
    – woodduck
    Commented Jul 30, 2016 at 11:10
1

This is my implementation of birgire's last suggestion (to parse the onclick):

jQuery(document).on( 'click', 'a.comment-reply-link', function( event ) {

// THIS PART GETS THE COMMENT ID
var hayStack = jQuery(this).attr('onclick');
var strawA = '"div-comment-';
var strawB = '"';
var tipNeedle = hayStack.lastIndexOf(strawA)+strawA.length;
var hayTruss = hayStack.substring(tipNeedle);
var endNeedle = hayTruss.indexOf(strawB); 
var needle = hayTruss.substring(0, endNeedle);
alert(needle);

// optional: THIS PART LOOKS UP THE PARENT ID (php side not shown)
var datatopost = {
        action: 'my_ajax_hook',
        nonce: ajaxobject.nonce,
        selected_comment_id: needle,
    };
jQuery.ajax({
    type: 'post',
    dataType: 'json',
    url: ajaxobject.ajaxurl,
    data: datatopost,
    error: function() { // Ajax request has failed
        jQuery('#comment').val('An error has occurred');
    },
    success: function (response) { // Ajax request has succeeded
        if (response.success) {
            // JSON successfully received
            alert(response.data.selected_comment_parent);
        }
    }
}); 
});

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.