Live Preview For Comments in Wordpress

February 12th, 2005

I added live preview to the comments today. It is actually quite simple. I use two functions in javascript. First, I added the following lines to wp-comments.php. I added them before the submit button, but you can put them wherever you would like the live preview to show on your page.

  1. <div id="live-preview">
  2. <div id="previewName"></div>
  3. <div id="previewComment"></div>
  4. </div>

Next, I included the following javascript functions in the head of my main wordpress page. If you didn't modify your wordpress install, your main page is index.php. My main page is articles.php.

  1. <script type="text/javascript">
  2. <!--//
  3. function previewComment() {
  4. var commentValue = document.getElementById("comment").value;
  5. newText = commentValue.split(/\n/).join("<br />");
  6. var commentObj = document.getElementById("previewComment");
  7. commentObj.innerHTML = newText;
  8. }
  9. function previewName() {
  10. var author = document.getElementById("author").value;
  11. var urlLink = document.getElementById("url").value;
  12. if (urlLink != '') {
  13. var text = '<h3><a href="' + urlLink + '">' + author + '</a> says:</h3>';
  14. } else {
  15. var text = '<h3>' + author + ' says:</h3>';
  16. }
  17. var nameObj = document.getElementById("previewName");
  18. nameObj.innerHTML = text;
  19. }
  20. //-->
  21. </script>

Lastly, I added a few onkeyup events to the comment form textarea and input boxes that use the live preview.

  1. <textarea name="comment" id="comment" cols="30" rows="10" tabindex="1" onkeyup="previewComment(); previewName();"></textarea>
  2. <input type="text" name="author" id="author" class="textarea" value="<?php echo $comment_author; ?>" size="28" tabindex="2" onkeyup="previewName();" />
  3. <input type="text" name="url" id="url" value="<?php echo $comment_author_url; ?>" size="28" tabindex="4" onkeyup="previewName();" />

Replace the above lines in your wp-comments.php and you should be good to go. In conclusion, first add the empty divs which will hold the live preview text. Second, include the javascript functions in the head of your main wordpress file. Lastly, add the onkeyup events to the appropriate comment form fields.

The key to this working is following the aforementioned steps and making sure that all the id's (of the inputs, textarea, preview divs) are the same as the id's in the javascript functions. To see the live preview in action check out the comment form below.

Update: I’m not using this for my comments anymore as I upgraded to 1.5 and just installed a plug-in.

7 Responses to “Live Preview For Comments in Wordpress”

  1. avatar alberto May 8th, 2006 12:49 pm

    direi che fa un bell’effetto…

  2. Nice colors. Keep up the good work. thnx!

  3. Sweet, even with images

  4. Excellent! This code works just fine.

    Just curious - what plugin are you using?

    I spent a couple hours searching, downloading and testing and I’ve yet to find anything as simple and effective.

    I tried:

    » Live Comment Preview v 1.7 (based on code from Jeff Mindard) by Iacovos Constantinou

    » Textile Live Preview (AJAX Version) v 0.2 by Joen Asmussen (also based on Jeff Minard’s code)

    » Filosofo Comments Preview v 0.78 by Austin Matzko
    This one isn’t AJAX-ified, but has a raft of other features and WordPress UI administration (that, frankly, I didn’t need)

    » AJAX Comments v 2.07 by Mike Smullin

  5. [...] Always trying to shortcut real work by downloading a ready-made plug-in, I went through at least four offerings before finding an excellent bit of code by John Nunemaker. [...]

  6. Sweet, even with images

  7. hi nice site.

About This Site

Addicted to New is the personal website of John Nunemaker (Noo-neh-maker), a Web Developer enamored of Ruby on Rails and a wide-eyed fan of all things new and cool.