Wordpress: how to write a reply to comment using jQuery

Friday, October 3rd, 2008

Recently Cristiano Up has released a useful plugin for Wordpress that can add two links to each comment in a post (maybe you have something similar installed, as in the directory, I trussed it Wordpress.org a couple, but as an Italian software would the event to honor the excellent work done by Christian). These links (reply & quote), meet and / or quote the author of a comment by performing the boring operation to insert the at sign (@) to indicate to whom they target. In this tutorial I will show - for the average user-experts - how to add these two features by hand, without resorting to installing any of Plugin. Also exploit the capabilities of jQuery to the party in Javascript. The technique I am raising is the same that I used to introduce this feature in this blog.

Note: The use of a plugin is the best solution for those who tend to frequently change their WordPress template or, at least, has in anticipation of doing so. This tutorial will only show how to perform targeted interventions within the code Wordpress and is dedicated to the most curious.

Edit the comments.php

The first change we make is to put the link "reply" and "units" within each comment. To do this edit the file comments.php in the root folder of our theme. This file contains all the instructions that allow you to view the comments at the end of a post. The section we are concerned (which may vary slightly from theme to theme) is the loop of creating various comments, identified by:

PHP:
  1. ( $comments as $comment ) : ?> <? php foreach ($ comments as $ comment):?>
  2. ?> "> <li id = "comment-<? php comment_ID ()?>">
  3. / / ...
  4. </ li>
  5. ; /* end for each comment */ ?> <? endforeach php / * end for each comment * /?>

In such a loop around a single comment tag is enclosed in li Immediately after the opening tag li or immediately before the closing tag li we can use our links, enclosing it in a div and set some classes that will help us to further define the layout:

PHP:
  1. <div class="jqr2c_ul">
  2. ?> ')">Rispondi</a> <a href="javascript:jqr2c_quote('comment- <?php comment_ID ( ) ?> ')">Quota</a> <a href = "javascript: jqr2c_reply ( 'comment-<? php comment_ID ()?>')"> Reply </ a> <a href = "javascript: jqr2c_quote ( 'comment-<? php comment_ID ()?>' ) "> Share </ a>
  3. </ div>

Note: we can improve our code also adding a check on the status of entries (open or closed) and on the type of comment (pingback, trackback, etc ...). For example entering:

PHP:
  1. ( comments_open ( ) && <? php if (comments_open () & &
  2. comment_type != "trackback" && $ comment -> comment_type! = "trackback" & &
  3. comment_type != "pingback" ) { ?> $ comment -> comment_type! = "pingback") (?>
  4. <div class="jqr2c_ul">
  5. ?> ')">Rispondi</a> <a href="javascript:jqr2c_quote('comment- <?php comment_ID ( ) ?> ')">Quota</a> <a href = "javascript: jqr2c_reply ( 'comment-<? php comment_ID ()?>')"> Reply </ a> <a href = "javascript: jqr2c_quote ( 'comment-<? php comment_ID ()?>' ) "> Share </ a>
  6. </ div>
  7. ?> <? php)?>

Edit the header.php

The links we put it in your comments.php called two JavaScript functions, in this case jqr2c_reply() and jqr2c_quote() We shall therefore include these functions in the header of the page, defined in the file header.php Edit this file and posizioniamoci before closing the tag head or before the call to WordPress wp_header() and insert the following lines of code:

PHP:
  1. <? php
  2. / **
  3. * Insert style and script only if we are viewing
  4. * A single post and the comments are open
  5. * /
  6. is_single ( ) && comments_open ( ) ) { ?> if (is_single () & & comments_open ()) (?>
  7. <style type="text/css">
  8. / * _____________ Here we define our styles * /
  9. div.jqr2c_ul (margin: 8px 0 30px 400px)
  10. / * Etc ... * /
  11. </ style>
  12. <script type="text/javascript">
  13. <!--//
  14. jqr2c_reply function (id) (
  15. aut var = jQuery ( 'li #' + id). children ( 'cite'). text ();
  16. jQuery ( 'textarea # comment'). text ( '<b> @' + aut + '</ b>:'). focus ();
  17. )
  18. jqr2c_quote function (id) (
  19. aut var = jQuery ( 'li #' + id). children ( 'cite'). text ();
  20. var c = jQuery ( 'li #' + id). children ( 'p'). text ();
  21. jQuery ( 'textarea # comment'). text ( '<b> @' + aut + '</ b>: \ n <blockquote>' + c + '</ blockquote> \ n'). focus ();
  22. )
  23. //-->
  24. </ script>
  25. ?> <? php)?>

If your blog, as in my case, already uses jQuery, this is all you need! Otherwise you can add these lines to the previous code after if (is_single() && comments_open())

PHP:
  1. is_single ( ) && comments_open ( ) ) { ?> if (is_single () & & comments_open ()) (?>
  2. / / Include jQuery through the Google Ajax Library API
  3. "text/javascript" src = "http://www.google.com/jsapi" ></ script > <Script type = "text / javascript" src = "http://www.google.com/jsapi"> </ script>
  4. "text/javascript" > google . load ( "jquery" , "1.2.6" ) </script> <Script type = "text / javascript"> google. Load ( "jquery", "1.2.6") </ script>
  5. / / ... otherwise identical ...

The two scripts include jQuery through the Google service explained in Google AJAX API Library: a turning point for developers

Related Post

Was this article helpful?: Per nientePocoAbbastanzaMoltoMoltissimo
Loading ... Loading ...

18 comments to "Wordpress: how to write a reply to comment using jQuery"

  1. getAvatar 1.0
    03 ott, 2008 camu:

    Excellent guide, as always!

  2. getAvatar 1.0
  3. getAvatar 1.0
    03 ott, 2008 Napolux:

    But why trouble jQuery (with all the weight that entails, although a minimum) when you can do everything by hand with simple JS?

  4. getAvatar 1.0
    03 ott, 2008 Napolux:

    Among other things here from you about Firefox 3.0.3 if the textarea has already "had" a focus buttons do not work ..

  5. getAvatar 1.0
    03 ott, 2008 Giovambattista Fazioli:

    @ Napolux:

    But why trouble jQuery (with all the weight that entails, although a minimum) when you can do everything by hand with simple JS?

    May be useful if, as in my case already using jQuery for other reasons. However, it is true that you can achieve it through a "simple" Javascript, but it is always useful to exploit the existing frame-work (jQuery, Prototype.js, Dojo, etc ...) mainly because ensure cross-browser compatibility is not always easy to implement (not in this case).

  6. getAvatar 1.0
    03 ott, 2008 Giovambattista Fazioli:

    @ Napolux:

    Among other things here from you about Firefox 3.0.3 if the textarea has already "had" a focus buttons do not work ..

    To me, if I understand correctly, he does not ... ? However it could be a worm fixare : D

  7. getAvatar 1.0
    04 ott, 2008 Christian:

    First I want to thank you very much for the review of the plugin which now also offers the possibility to move between comments, in addition to the features of Quote and Reply.
    Unfortunately, although I have been advised several times by others, I have not yet been reported (along with others that I realized) the repository of WordPress.org.
    I will do as soon ;-)

    Regarding the proposed solution is definitely the best if you already are using jQuery, the only handicap is that it seems to me that does not allow to list a partial selection of the text.

    For those interested in an alternative solution using pure javascript I would point out the first article I wrote recently on related subjects, and from which then (after many requests for readers) came up with the implementation of the plugin:

    A procedure for automatically quoted comments in a post

    The procedure also allows the partial quoting the text and is cross-browser.

    A warm greeting to Giovambattista with whom I share not only the passion for computing and programming (I too am the father of twins :-) )

  8. getAvatar 1.0
    06 ott, 2008 Giovambattista Fazioli:

    @ Christian: you're right on handicap in the selection, intentionally avoided, because I had a purpose, "teaching" and / or descriptive and not replace operation of your great plugin : D
    Agree on a passion for programming and "difficult" to grow twins : D: D
    If you have any idea for a new Wordpress plugin I'd like to work with you if you wish!
    Greetings and see you soon

  9. getAvatar 1.0
    06 ott, 2008 Giovambattista Fazioli:

    @ Napolux:

    Among other things here from you about Firefox 3.0.3 if the textarea has already "had" a focus buttons do not work ..

    The problem is the use of the method text() to jQuery. This works until there is added new content. Better to use the form attr('value')

  10. getAvatar 1.0
    06 ott, 2008 Christian:

    @ Giovambattista Fazioli:

    I had a goal "educational" and / or descriptive and not replace operation of your great plugin

    I guessed ;-)

    If you have any idea for a new Wordpress plugin I'd like to work with you if you wish!

    The thing could only give me an immense pleasure, definitely take advantage of your skills and knowledge in :-)

  11. getAvatar 1.0
  12. getAvatar 1.0
    06 ott, 2008 Christian:

    @ Giovambattista Fazioli:
    I have already "seen" and "verified" by the referrer :-)
    I finally found time to create me an account on the repository and submit the WordPress plugin: I do not know what it will take to approve ;-)
    If it will not take much time, then others also suggest that I have already achieved.

  13. getAvatar 1.0
    09 ott, 2008 Leonaut.com:

    Wordpress: how to write a reply to comment using jQuery ...

    Cristiano Until recently released a useful plugin for Wordpress that can add two links to each comment in a post (maybe you have something similar installed, as in the directory, I trussed it Wordpress.org a couple, but as ...

  14. getAvatar 1.0
    December 14, 2008 PanHack:

    Hello you are very good ... excellent guides!

    Congratulations ;)

    I have a blog I anke ==> If you go Visitagao:

    PanHack-Blog

  15. getAvatar 1.0
    December 15, 2008 camu:

    love people who write "Anke" :) only just for that I'll never go to visit your blog, my dear PanHack!

  16. getAvatar 1.0
    December 15, 2008 Giovambattista Fazioli:

    @ camu:

    I love people who write "Anke" only just for that I'll never go to visit your blog, my dear PanHack!

    :)

  17. getAvatar 1.0
    December 15, 2008 camu:

    Sorry Giovambattista, I woke up a bit 'acid this morning : D

  18. getAvatar 1.0

Leave a comment

TAG XHTML PERMISSIONS: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre>         // blocco generico [code][/code]       // blocco generico [as][/as]           // Actionscript [css][/css]         // CSS Style Sheet [html][/html]       // HTML [js][/js]           // Javascript [objc][/objc]       // Objective-C [php][/php]         // PHP [sql][/sql]         // SQL