• Resolved sarahjadesigns

    (@sarahjadesigns)


    Before I start, I would like to mention that I know just about nothing about PHP. I’ve been able to figure most things out with the help of these forums and Google, but I’ve now hit a wall with something.

    I’ve set up my comment form to appear on the click of a link/button with Javascipt:

    <a href="#respond" onclick="toggle_visibility('respond');">Show / Hide Comments</a>
    
    <script type="text/javascript">
        function toggle_visibility(id) {
           var e = document.getElementById(id);
           if(e.style.display == 'block')
              e.style.display = 'none';
           else
              e.style.display = 'block';
        }
    </script>

    In order to make the form visible when a user clicks to reply to an existing comment, I need to add the onclick event to the Reply link as well.

    I believe I found the place where I could do this in the core, but I obviously don’t want to edit the core files directly. This appears to be the relevant code starting line 1061 of comment-template.php:

    $link = "<a class='comment-reply-link' href='" . esc_url( add_query_arg( 'replytocom', $comment->comment_ID ) ) . "#" . $respond_id . "' onclick='return addComment.moveForm(\"$add_below-$comment->comment_ID\", \"$comment->comment_ID\", \"$respond_id\", \"$post->ID\")'>$reply_text</a>";
    	return apply_filters('comment_reply_link', $before . $link . $after, $args, $comment, $post);

    My question is: How do I add my onclick event to this code without editing the core file? Or if this not the correct code, then what is it and how do I go about doing it then?

    Any help would be much appreciated.

Viewing 4 replies - 1 through 4 (of 4 total)
  • I think this is a case where JQuery comes to the rescue rather than PHP.

    If there isn’t already an onclick event for the comment reply link, according to your code snippet above, the comment reply link has a class of “comment-reply-link.”

    Using JQuery you can add the following to your child theme’s javascript:

    $(document).ready(function() {
    	$('.comment-reply-link').click(function() {
    		toggle_visibility('respond');
    	});
    });

    As long as JQuery is loading for your site, it will add the toggle_visibility function to all the links with the class “comment-reply-link.”

    Note that you don’t need the $(document).ready part if you’re already adding the code within a $(document).ready section of your theme’s javascript. This will only work if your theme or another plugin is loading the JQuery library.

    By the way, there’s an easier way to show/hide the form using JQuery, too, than the javascript method you’re using. Take a look at the JQuery toggle() method.

    Thread Starter sarahjadesigns

    (@sarahjadesigns)

    Thanks for the response and the tip about the JQuery toggle() method. Scripting really isn’t my strong suit, so I just google until I find something I know how to implement.

    You said the code you suggested can be added if there is no other onlick event, but there is one already if you scroll to the right to see the rest of my code above:

    onclick='return addComment.moveForm(\"$add_below-$comment->comment_ID\", \"$comment->comment_ID\", \"$respond_id\", \"$post->ID\")

    I believe this is what moves the comment form from its default location to underneath the comment you’re replying to.

    Would there be a solution with JQuery in this case as well?

    I haven’t tested it, but I think it may still work. I think jQuery will add the additional function (toggle_visibility) and leave the existing ones in there too.

    Give it a shot and let us know.

    Thread Starter sarahjadesigns

    (@sarahjadesigns)

    Woot! It works! And the moveForm event is still working too.

    Thanks a bunch!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Editing the Reply link code’ is closed to new replies.