Ajax Comments — How I did It September 20, 2005

Theron Parlin

One of the first questions asked of me was to make this into a plugin. Packaging this up as a plugin will be tough because it requires some detailed additions to your theme. You have to add javascript includes to your header file, you have to heavily modify the ‘form’ tag on the comments page, as well as add error and loading divs. Not to mention, the solution requires that your comments be inside ‘ol’ tags. That’s a recipe for disaster as a plugin and a support nightmare. The best I can do is offer the files for download and explain how I got it working and the rest is up to you.

Step 1: Download the squible theme and grab the following files:

ajax_comments.php
scripts/ (directory contains ajax_comments.js prototype.js and effects.js)

Step 2: Put these files in your theme directory:

wp-content/themes/yourtheme/ajax_comments.php
wp-content/themes/yourtheme/scripts/

Step 3: Add this code to your header.php file:

<script type=”text/javascript” src=”/wp-content/themes/squible/scripts/prototype.js”></script>
<script type=”text/javascript” src=”/wp-content/themes/squible/scripts/effects.js”>

Step 4: Replace your comments.php with the one from squible

If you don’t want to do step 4, then just change your form tag to look like so:


<form id="commentform" onsubmit="new Ajax.Updater({success:'commentlist'}, '<?php bloginfo('stylesheet_directory') ?>/ajax_comments.php', {asynchronous:true,
evalScripts:true, insertion:Insertion.Bottom, onComplete:function(request){complete(request)}, onFailure:function(request){failure(request)}, onLoading:func
tion(request){loading()}, parameters:Form.serialize(this)}); return false;">
</form>

I’d like to give a big nod of acknowledgement to siriux.net. I couldn’t get his solution to work, but his page was a great starting point.

Article printed from Squible: http://www.squible.com
URL to article: http://www.squible.com/2005/09/20/ajax-comments-how-i-did-it/

Tags
Conversation
Related Tags
Comments
Trackback

    44 Responses to “Ajax Comments — How I did It”

  1. Chris J. Davis September 20th, 2005 at 2:58 pm | Permalink

    Interesting, I have been considering playing around with this type of thing for a while now.

    Looks like I have a place to start.

  2. jarkolicious » Wordpress AJAX Comments September 21st, 2005 at 6:34 am | Permalink

    […] theme release. by Scott | posted in Asides Trackback URL | Comment RSS Feed Tag at del.icio.us | Incoming links tagged as wordpress,ajax […]

  3. Ensellitis September 21st, 2005 at 12:41 pm | Permalink

    Thanks! Been looking for something like this

  4. Theron Parlin September 21st, 2005 at 12:44 pm | Permalink

    You’re Quite welcome Ensellitis. :)

  5. XeroCool September 22nd, 2005 at 4:17 pm | Permalink

    Nice

  6. Joe September 24th, 2005 at 9:42 am | Permalink

    Very good tutorial! When I have some time…I am planning on implementing it on my site, also.

  7. Nico September 24th, 2005 at 6:33 pm | Permalink

    Nice! Almost the same as I did it ;-)
    And it seems to be included in K2 as well.

    However this version will break if JavaScript is not enabled. To allow non-JS users to still be able to post comments, keep the action="/wp-comments-post.php" method="post" in the form tag. JS users still have the full Ajax experience, the return false in the onsubmit tag does the magic.

  8. Theron Parlin September 24th, 2005 at 11:53 pm | Permalink

    Hi Nico, I don’t know if you noticed, but I acknowleged your solution in my post. If it wasn’t for your solution, I wouldn’t have gotten mine to work! :)

  9. Trevor September 25th, 2005 at 12:15 am | Permalink

    Cool!

  10. Nico September 25th, 2005 at 2:51 am | Permalink

    Theron Parlin: sure I acknowledged, that’s why I’m happy (you even posted a Trackback to my site) :-)

  11. Denis September 25th, 2005 at 6:14 am | Permalink

    awesome ! No when will i have time to add this god knows !

  12. Chris Gwynne » WordPress AJAX Commenting September 26th, 2005 at 12:35 pm | Permalink

    […] WordPress AJAX Commenting Not a plug-in but a way to integrate it in to your existing theme. by Scott | posted in Asides Trackback URL | Comment RSS Feed Tag at del.icio.us | Incoming links […]

  13. tinyau September 28th, 2005 at 1:40 am | Permalink

    I’ve tried it in my testing environment and found a problem. My blog is Chinese under UTF-8 encoding. After I posted the comment using Chinese, the Chinese characters become rubbish characters. I have to refresh the whole page in order to view the correct Chinese characters.

    P.S. There is no problem when I use English.

  14. Nico September 28th, 2005 at 2:15 am | Permalink

    tinyau: I just tested it on http://siriux.net/news/2005/07/13/ajax-commenting/ (copy and paste your chinese characters) and it worked perfectly, even without refreshing. Did it work for you too there?

  15. tinyau September 28th, 2005 at 9:29 am | Permalink

    Nico, it works there but I don’t know why it failed in my testing environment.

  16. gummy September 28th, 2005 at 2:45 pm | Permalink

    hi. pls advise. i want to know which file i need to modify to change the background color of this theme (red, black) and pls tell me also which files to work with if i want to play with the fonts and background’s color. will highly appreciate your replies

  17. Theron Parlin September 28th, 2005 at 2:53 pm | Permalink

    Gummy, please use the discussion forum for these types of questions.

  18. otama October 4th, 2005 at 6:03 pm | Permalink

    hello, ive tryed several ways, but i cant make it work, even replacing my comments.php with yours…
    any advice?
    any obscure option that i should edit? i cant see where’s the problem, and i’d love to get it working :(

  19. Nico October 4th, 2005 at 6:34 pm | Permalink

    otama: Did you try to disable the scrobbler plugin?

  20. otama October 4th, 2005 at 6:41 pm | Permalink

    I need to disable it? duh… >_

  21. otama October 4th, 2005 at 6:47 pm | Permalink

    Ive tried to disable it, but it keeps reloading the whole page…

  22. Nico October 4th, 2005 at 7:01 pm | Permalink

    otama: ok, scrobbler seems to be something else ;-)
    The problem are the lines in the header of your template. Move the “script” elements into the “head” element (e.g. right under the “meta name=generator” element) and replace the quotes with real quotes (if you copy&pasted them from here they may be wrong).

  23. otama October 4th, 2005 at 7:16 pm | Permalink

    lol, the quotes where wrong… lots of thanks for that.

    now it does not load the page again, but the comments doesnt show… nothing happens when i hit the “send comment” button, but if i reload the page, the text apears…

    sorry for bothering :_(

  24. otama October 4th, 2005 at 7:19 pm | Permalink

    maybe theres something wrong in the form tag?

    /wp-comments-post.php" method="post" id="commentform" onsubmit="new Ajax.Updater({success:'commentlist'}, 'http://www.diauno.com/wp-content/themes/scattered/ajax_comments.php', {asynchronous:true, evalScripts:true, insertion:Insertion.Bottom, onComplete:function(request){complete(request)}, onFailure:function(request){failure(request)}, onLoading:function(request){loading()}, parameters:Form.serialize(this)}); return false;" >

  25. otama October 4th, 2005 at 7:20 pm | Permalink

    ok, i cant post code, sry :(

  26. otama October 4th, 2005 at 8:20 pm | Permalink

    could have i some kind of javascript error? :(

  27. Nico October 5th, 2005 at 5:09 am | Permalink

    otama: you are missing the “errors” div element where errors are displayed. Look at the HTML code of this page e.g. to see what I mean. And, to avoid the next problem, the “commentlist” ol element must be displayed even if there is no comment. DíaUno does not seem to display it when there is no comment.

    (Well, I didn’t say it’s easy to integrate ;-))

  28. br0k3n_d4y October 6th, 2005 at 5:20 am | Permalink

    just a test

  29. W. Andrew Loe III October 7th, 2005 at 10:18 am | Permalink

    Hot, I want this.

  30. Jono October 23rd, 2005 at 6:41 pm | Permalink

    also, testing. sorry!

  31. Rehan November 1st, 2005 at 7:41 am | Permalink

    Has the issue regarding comment number not being shown as 1 been resolved?

  32. Ravi November 2nd, 2005 at 9:35 pm | Permalink

    Testing

  33. abcd November 3rd, 2005 at 8:15 pm | Permalink

    teeeeeeeeesting

  34. Vitor November 6th, 2005 at 12:29 pm | Permalink

    Testing 1, 2, 3

  35. test November 7th, 2005 at 8:13 pm | Permalink

    test

  36. N. November 9th, 2005 at 5:19 pm | Permalink

    Doesn’t look like your Ajax comments work when there is a ” ” sign in the email address section, otherwise this is a brilliant implementation.

  37. N. November 9th, 2005 at 5:19 pm | Permalink

    Hmm, looks like it got rid of the plus sign all together. But that is what I was talking about, the plus sign in the email address messes up the ajax comments.

  38. Piggy November 10th, 2005 at 6:43 am | Permalink

    Very clever. Excellent, in fact.

    Well done.

  39. Ka-Meng November 10th, 2005 at 8:48 pm | Permalink

    I’m intrigued as to how the comment number is inceremented when the Ajax comment is posted on Squibble, while it does not work on K2, which only shows comment no 1, until you refresh the pages that is.

    Any ideas or clues to why this happens? Thanks.

  40. Theron Parlin November 10th, 2005 at 11:28 pm | Permalink

    K2 is probably manually numbering the comments, while I’m using <ol>.

  41. Ka-Meng November 11th, 2005 at 1:14 am | Permalink

    I just had a look at K2 comments.php and see that it does use the <ol> tags. However, I found that a separate ordered list is created for the pingbacks. So could that be the case?

  42. Theron Parlin November 11th, 2005 at 9:13 am | Permalink

    Hmm, that’s hard to say. I honestly don’t know — have you tried asking in the k2 forums?

  43. Ka-Meng November 11th, 2005 at 6:24 pm | Permalink

    Theron, it’s an acknowledged bug in K2 and I’m not sure how far they got to having it fixed. So was trying to figure it out myself to see if it’s fixable. But I’m no PHP guru and these count variables are driving me up the wall. Thanks for your help!

  44. La mate por un yogur » Tus comentarios con Ajax November 13th, 2005 at 3:59 pm | Permalink

    […] Explicación para convertir tus comentarios a la actual web 2.0 mediante Ajax. Recuerden hacer una copia de seguridad antes de tocar el código de vuestro theme. Wordpress, Miniblog | Enlace permanente # | […]

This entry was posted on Tuesday, September 20th, 2005 at 2:11 pm. You can follow any responses to this entry through the RSS 2.0 feed. If you're wondering how to get your own icon next to your comment, go visit gravatar.com and get yourself hooked up.