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.
<div id="live-preview"><div id="previewName"></div><div id="previewComment"></div></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.
<script type="text/javascript"><!--//function previewComment() {var commentValue = document.getElementById("comment").value;newText = commentValue.split(/\n/).join("<br />");var commentObj = document.getElementById("previewComment");commentObj.innerHTML = newText;}function previewName() {var author = document.getElementById("author").value;var urlLink = document.getElementById("url").value;if (urlLink != '') {var text = '<h3><a href="' + urlLink + '">' + author + '</a> says:</h3>';} else {var text = '<h3>' + author + ' says:</h3>';}var nameObj = document.getElementById("previewName");nameObj.innerHTML = text;}//--></script>
Lastly, I added a few onkeyup events to the comment form textarea and input boxes that use the live preview.
<textarea name="comment" id="comment" cols="30" rows="10" tabindex="1" onkeyup="previewComment(); previewName();"></textarea><input type="text" name="author" id="author" class="textarea" value="<?php echo $comment_author; ?>" size="28" tabindex="2" onkeyup="previewName();" /><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.
If you enjoyed this post, get free updates by email or RSS.
direi che fa un bell’effetto…
Nice colors. Keep up the good work. thnx!
Sweet, even with images
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
[...] 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. [...]
Sweet, even with images
hi nice site.