• I need to get the comment_id of the comment when ‘Reply’ is clicked on that comment. I would like the comment_id stored in a global variable at that time, accessible via a javascript call, triggered by the “editMe()” call set out below.

    I have a button id “btnLink” embedded in comment-template.php at:

    $defaults = array(
    //...
    'comment_field'        => '<p class="comment-form-comment"><div><input id="btnLink" type="button" onclick="editMe()" value="displayCommentInfo" /></div>
        <label for="comment">' . _x( 'Comment', 'noun' ) . '</label>
        <textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" aria-required="true" required="required">
        </textarea></p>',

    The onclick function “editMe()” is running in a javascript file that is loaded at runtime when the page is loaded.

    When the user clicks the button, the call is made to “editMe()”, and when the call is made, I want to get the comment_id value for the (parent) comment that would be stored in the global variable when ‘Reply’ is clicked.

    IOW, after ‘Reply’ is clicked and the WordPress comment form is displayed, when the user clicks the button ‘btnLink’, the called function would have access to the immediate parent comment_id of that reply.

    Can you help, please?

    Thanks
    Jimmy W

    [Moderator note: code fixed. Please wrap code in the backtick character or use the code button. Failure to do this makes your code difficult to test.]

Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator bcworkz

    (@bcworkz)

    There are a number of ways to pass a value to JavaScript that is known to PHP. They are all variations of outputting the value within page script somewhere. I don’t have enough context to recommend the best method. One thing you can do is pass the value as a parameter to editMe(). You probably need to alter how the onclick attribute is output so that it contains the correct ID. I doubt it’s available when $default is declared. For the sake of example, let’s assume it is available through get_the_ID().
    ... onclick="editMe('. get_the_ID() .')" ...

    Then of course in the editMe() declaration you would collect the passed value:
    function editMe( parentId ) { //...

    Thread Starter punchlines

    (@punchlines)

    Thank you for your reply. get_the_ID() doesn’t return the comment_id.

    Can you show me how to get the comment_id of the parent comment for the active reply? Even if get_the_ID() worked, it would return the ID of the current comment, which is the reply, not the parent. And, of course, since the reply has not yet been posted, it would return zero.

    This should be simple. For example, I can readily get the $postID of the page simply this way: echo $post_id;

    So I would have thought that, if $comment_id is a valid variable (which it is, of course), then there must be an equally simple way to grab it. I’m just not experienced in PHP as I’d like to be, particularly how to pass PHP variables to a javascript call.

    Thanks
    Punch

    Moderator bcworkz

    (@bcworkz)

    Well, get_the_ID() was just an example to illustrate how to pass a returned value to JS. Maybe it was not a good choice even just as an example. Getting the correct ID you need is independent from how it would be passed. So lets look at that.

    Without some context, I couldn’t say exactly how to get the parent ID. Generally speaking, any comment’s parent ID is available from the WP_Comment object. WP_Comment::comment_parent. If the current comment object is available as $comment, pass the parent ID like so:
    ... onclick="editMe('. $comment->comment_parent .')" ...

    If all you have is the current comment ID, not the object, you can get the object with get_comment( $comment_id );. Even without an ID, get_comment() will try to get the current comment object through internal WP globals. If the context is appropriate, then you may be able to pass the parent ID like so:
    ... onclick="editMe('. get_comment()->comment_parent .')" ...

    You should do some error trapping though, if not in PHP, then in JS. get_comment() could return null. Trying to get the parent property of null will of course generate an error. If the comment object has no parent, the comment_parent property will be 0, which would require different handling.

    Thread Starter punchlines

    (@punchlines)

    I’ll try to understand your info. Thanks for that..

    But here’s what’s peeing me off: In the HTML, the number I want is readily apparent – number 381, as a parameter being passed in the reply onclick event:

    <div class="reply"><a rel='nofollow'>381</strong>#respond' onclick='return addComment.moveForm( "div-comment-381", "<strong>381</strong>", "respond", "2956" )' aria-label='Reply to Fred to Fred D'>Reply to Fred</a></div></article><!-- .comment-body --></li><!-- #comment-## -->

    If I could modify the onclick event ‘return addComment.moveForm(‘ in comment-template.php so that it also put the value into a special <div> I have on the page, all would be solved. I could then read the innerHTML of that div in javascript:

    I have this div on the page:

    </script><div style="visibility:visible" id="divCommentID"></div></script>

    So if there’s a way to update innerHTML at the same time as the onclick event is fired, I can then easily read the innerHTML in a js call.

    Do you have any suggestions how I can do this?

    Thanks
    Fred

    • This reply was modified 8 years, 10 months ago by bcworkz. Reason: code fixed
    Thread Starter punchlines

    (@punchlines)

    Correction:

    <script><div style=”visibility:visible” id=”divCommentID”></div></script>

    Thread Starter punchlines

    (@punchlines)

    And… this works to fetch the page id”

    onclick=”editMe(‘. $post_id .’)”

    But none of the other suggestions seem to work. They all return “undefined”.

    Any help would be mostttttt appreciated 🙂

    Fred

    Moderator bcworkz

    (@bcworkz)

    If you post the comment query and loop setup code responsible for the HTML output that includes your onclick attribute, I can suggest something that should work. Without such context there’s little I can do.

    If the ID is passed as the second parameter in another onclick attribute, you could locate the code generating that output and use it yourself. This will only work if your output is within the same variable scope.

    Please use backticks or the code button when you post code. You will not be able to format specific parts of the code, but it’s a small price to pay to have code that is readable and usable for testing.

    Not that it matters, but you might consider being consistent in the sign off name you use within one thread, Fred/Punch/Jimmy 😛 I get the desire to use an alias, and that’s fine, I’ve used a few at times myself. But all in one thread? You’re better off not using a sign off at all 🙂

    Thread Starter punchlines

    (@punchlines)

    Thanks for the info. I don’t know what backticks are or how to insert them.

    The code posted is not meant for anything other than viewing, so I’m not concerned about usability. I didn’t see any trouble reading it. Can you highlight some areas that you weren’t able to read, pls?

    Why is there concern about my sign off name? Are there rules about it?

    Thanks
    F

    Moderator bcworkz

    (@bcworkz)

    Using backticks or the code button is part of the forum guidelines. A backtick is a character on your keyboard somewhere. There is no consistent location. Place one at either end of your code like you might when quoting text: `a code snippet` will render as a code snippet Or highlight your code and click the code button.

    Your entire code was not visible until I fixed it because parts of it where parsed as HTML. In fact, the onclick attribute itself was not visible. While you may not intend your code to be usable, it’s still common for volunteers here to execute snippets to reliably identify syntax or logic errors or otherwise debug code. While you intended the code as a mere example, it’s possible your code was fine except the quotes didn’t balance or something. Finding such errors visually in nested quotes can be difficult, whereas code editors find them quickly. But code editors do not always balance illegal characters like the curly quotes that replace your straight quotes when you do not use backticks.

    Everyone here generously volunteers their time to help others. Please make the small effort to make it a bit easier for other volunteers to help you. In fact, some volunteers are more likely to skip over posts containing unformatted code.

    Believe it or not, there actually is a guideline against any sign off or signature of any sort, even if you’re consistent. Despite that, the guideline is not my real concern. Inconsistent aliases cast a small amount of suspicion on your account, as these forums have some issues with members misrepresenting themselves as someone more credible.

    I am in no way accusing you of misrepresentation, in truth I was mostly just poking you a bit in jest. But there was a small thread of seriousness there too. We often need to rely on subtle clues to identify abuse. In the worst case, your posts could get caught up in the automated flagging system, causing a delay until someone reviews your post and sees that all is fine. Hardly the end of the world, but I thought you might want to avoid any chance of it anyway.

    Continue on if you must. In the future, if your posts don’t immediately show up, this could be why. Wait a while and someone will release your post.

    Thread Starter punchlines

    (@punchlines)

    Got it.. thanks

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Store comment_id in global variable when reply clicked then read with javascript’ is closed to new replies.