by ShibaShake


Screen-shot of the comments section of an example static webpage (generated using Spry).

There are four key steps to making a Spry comment box for a static web page.

  1. Extract relevant comment data and output results in an XML format.
  2. Display comment data using a Spry region.
  3. Display a Spry interface for entering a new comment.
  4. Store the new comment data.

In this article we will focus on steps 2 and 3 – displaying comments and collecting new comment information.

The easiest way to execute steps (1) and (4) is by linking your Spry comment box to a local blog system such as WordPress. By doing this, you can tap into the existing comment moderation and spam detection functionality of WordPress without having to reinvent the wheel.

In addition, it is fairly easy to link into WordPress using PHP scripts that can then be called directly from your Spry comment box. For an example on how to generate such an XML file, check out Displaying WordPress Comments in a Static Web Page.

Before creating a Spry comment box, note that Spry displays comments on a web-page using Javascript. As a result, your comments will not be visible to search engines. If you want your comments to be available for search engine optimization purposes, use PHP scripts instead of Spry.

1. Load Spry Data Set

1
2
3
4
5
6
7
8
<script type="text/javascript" src="SpryAssets/xpath.js"></script>
<script type="text/javascript" src="SpryAssets/SpryData.js"></script>
 
<script type="text/javascript">
var dsComments = new Spry.Data.XMLDataSet("sample.php?" + "d=" + (new Date()).getTime()", "comment-set/comment");
 
dsComments.setColumnType("commentDate", "date");
</script>

The first step is to load your comments data set.
Lines 1-2 – includes the Spry library so that you can call Spry functions within your HTML file.

Line 5 – loads in your comments data set. This is done by calling a PHP function that will read in your comments and then generate a valid XML file.

The sample XML code below shows an example of the XML file structure that we expect.

Note that in addition to linking to the PHP file, we also pass in the current date as an argument. The reason we do this, is to force the Spry data set to reload new data from the PHP file every time the page is reloaded. Otherwise, certain browsers may just cache the data set results and not update your comment box with new data.

+ "d=" + (new Date()).getTime()

Instead of passing the Spry function a PHP file we can also pass it a fixed XML file.

After reading in your comments data set, you can set the data types for the different fields. In line 7 we set the commentDate field of our data set to contain date data types.

All of this code should go into the header section of your HTML file.

Sample XML Data Set

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="utf-8"?>
<comment-set>
 
<comment>
<id>comment-1002</id>
<commentDate>July 26, 2009</commentDate>
 
<cite><![CDATA[<a href="http://shibashake.com">shibashake</a>]]></cite>
 
<avatarimg><![CDATA[<img alt='' src='http://www.gravatar.com/avatar/?d=monsterid&amp;s=45' class='avatar avatar-45 avatar-default' height='45' width='45' />]]></avatarimg>
 
<text><![CDATA[Test Text]]></text>
</comment>
 
</comment-set>
  1. id contains a unique identifier for the comment.
  2. commentDate contains the date when the comment was originally posted.
  3. cite contains a link to the author of the comment. If the comment author does not have an associated website URL, then it will just contain the author name.
  4. avatarimg points to the avatar image of the comment author.
  5. text contains the textual contents of the comment.

2. Display Comments From Data Set

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<div id="comments-table" spry:region="dsComments">
<div spry:repeat="dsComments">
	<table width="100%" border="0">
	<tr>
		<td width="45"><a name={id} id={id}></a>{avatarimg}</td>
		<td class="subHeader">{cite}</td>
		<td width="200"><div align="right"><em>{date}</em></div></td>
	</tr>
	<tr>
		<td colspan="3">{text}</td>
	</tr>
	<tr>
		<td colspan="3">&nbsp;</td>
	</tr>           
	</table>
</div>
</div>

In the body of your HTML file, place the code above.

Line 1 – will create a Spry region that has access to the data set we just loaded.
Line 2 – will repeat displaying the table within it for each comment object in our data set.
Lines 3-15 – specifies the table structure of our comment.

<a name={id} id={id}></a>

This HTML fragment in line 5, sets an anchor for the comment, so that we can easily jump to specific comments within the HTML page. We assume that {id} contains a unique identifier for the comment.

This will display all the comments in our data set.

Here is an example.



Example Spry table structure within the Design view of Dreamweaver CS3




Example screenshot of Spry comment display.

3. New Spry Objects for Posting Comments

1
2
3
4
5
6
<script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
<script src="SpryAssets/SpryValidationTextarea.js" type="text/javascript"></script>
 
 
<link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
<link href="SpryAssets/SpryValidationTextarea.css" rel="stylesheet" type="text/css" />

We include two new Spry objects for our post new comment interface, SpryValidationTextField and SpryValidationTextarea.

Include the code above into the header of your HTML file to get access to these two new Spry interface objects.

4. Display Post Comment Form

The comment form below contains standard HTML form elements except for two Spry objects – a validation text field (sprytextfield1) for the author of the comment and a validation text area (sprytextarea1) for the comment itself.

These Spry validation objects will ensure that a new comment contains at least a comment author, as well as actual comment content. The e-mail and website fields are optional, therefore they are defined using the regular HTML input tags.

When the user presses the Submit button within the form, the file specified on the action tag of the form (i.e. wp-comments-post.php) will get executed.



Example screenshot of the Spry post comment interface.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<form id="commentform" name="commentform" method="post" action="wp-comments-post.php">     
 
 
<div id="formregion"> 
	<table width="100%" border="0">
 
	<tr> // Input comment author name
		<td width="50">Name</td>
		<td>
		<span id="sprytextfield1">
			<label><input type="text" name="author" id="author" /></label>
			<span class="textfieldRequiredMsg">Please enter your name.</span>
		</span>
		</td>
	</tr>
 
	<tr> // Input comment author e-mail
		<td>E-Mail</td>
		<td><input type="text" name="email" id="email" /></td>
	</tr>
	<tr> // Input comment author website
		<td>Website</td>
		<td><input type="text" name="url" id="url" /></td>
	</tr>
	<tr> // Input comment text
		<td colspan="2">
		<span id="sprytextarea1">
			<label><textarea name="comment" id="comment" cols="70" rows="5"></textarea></label>
			<span class="textareaRequiredMsg">Please enter your comment.</span>
		</span>
		</td>
	</tr>
	</table>
</div> <!--end formregion--> 
 
<div class="buttons">
	<input type="submit" name="Submit" id="submit" value="Submit"/>
	<input type="reset" name="Reset" id="reset" value="Reset" />
</div>
 
</form>
 
<script type="text/javascript">
<!--
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1");
var sprytextarea1 = new Spry.Widget.ValidationTextarea("sprytextarea1");
//-->
</script>

5. Post Comment to WordPress

wp-comments-post.php is actually a WordPress file. It is used by WordPress blogs to post comments. To properly link your Spry comment form to wp-comments-post.php, you must name your input HTML objects in the same way specified above. In particular,

  1. author should contain the commenter’s name.
  2. email should contain the commenter’s electronic mail address.
  3. url should contain the commenter’s website URL.
  4. comment should contain the comment’s text.

In addition to author, email, url, and comment, you will need to define two additional values.

  1. redirect_to which points to the file your browser should load after posting the new comment.
  2. comment_post_ID which contains the page or post ID that WordPress should associate with the comment.

Below we set the redirect_to parameter to the current HTML page so that your browser will reload the current static web page after it posts the new comment. We set comment_post_ID to the WordPress page we want to associate with our current static web page.

1
2
3
4
<script>
document.write('<input type="hidden" name="redirect_to" id="redirect_to" value="' + window.location.href + '"/>')
document.write('<input type="hidden" name="comment_post_ID" id="comment_post_ID" value='+ pageID + '/>')
</script>

6. Get WordPress Page ID

You can obtain the page ID for any WordPress page by using the PHP script below.

Line-3 – loads the WordPress function library. get_page_by_title is a WordPress function that allows you to retrieve a WordPress page based on its title.

1
2
3
4
5
6
7
<?php
	// Load WordPress functions
	require('wp-load.php'); 
 
	$post = get_page_by_title('WordPress Page Title'); 
	echo $post->ID;
?>

7. Redirect User to the New Comment

Finally, the last thing you need to do is ensure that you redirect the user to their new comment after they have made their submission.

This process is slightly more complex than usual because your Spry comment table is dynamic and will not be populated yet when the page is first loaded. As a result, you need to separately check if there is an anchor (i.e., #) in the URL address after your Spry comment table is fully populated. If so, redirect the browser to the anchor location using the PHP code below.

You can place this javascript code right below your Spry.Data.XMLDataSet command.

1
2
3
4
5
6
7
8
Spry.Data.Region.addObserver("comments-table",{onPostUpdate:function(){
	// check if theres a anchor has in the url example.com/#example
	if(location.hash){
	window.location = location.hash; // go to the hash location
	}
}
});
</script>

That’s All Folks

Now you should have your very own Spry comment box that you can insert into any static web page. Feel free to leave me questions and comments if you should run into any errors.

I have a Spry comment box here if you want to test it out. Note that it is a static web-page with a html extension. This current page is just a regular WordPress page.

Related Articles

<< Previous Next >>

<a href="http://shibashake.com/wordpress-theme/spry-comment-box-test-page" target="_top">Spry Comment Box Test Page</a>

Spry Comment Box Test Page

[How to create a Spry comment box in a static web-page.] [Example Spry comment box static web-page.]

<< Previous Next >>

<a href="http://shibashake.com/wordpress-theme/display-wordpress-comments-in-a-static-web-page" target="_top">Display WordPress Comments in a Static Web Page</a>

Display WordPress Comments in a Static Web Page

Why would you want to link WordPress comments into a static web page? By linking your static web pages to WordPress comments, you get to utilize all of WordPresss existing functionalities for moderating comments as well as for detecting spam comments. You also save yourself a lot of effort from writing all the backend functions for linking with a...

<< Previous Next >>

<a href="http://shibashake.com/wordpress-theme/wordpress-comments" target="_top">WordPress Comments</a>

WordPress Comments

WordPress Comments Generate Your Own Avatars for Your WordPress Blog Unhappy with the Monsters, Wavatar, or Identicon avatars that come with WordPress? Personalize your blog comments with your own set of avatar images. Displaying WordPress Comments in a Static Web Page How to extract comments from the WordPress database and format them into...

<Playback Stop Play >

15 Comments

  1. hey I was trying to figure out a way to have my comments drop down from the post when someone mouses over it and then retract when they mouse over a different post (all my posts are displayed on one main static page+comments right now). I guess I’m not sure exactly what spry is capable of. Any ideas? Thanks a lot

    10:30 pm on March 1st, 2010 Reply
  2. Alvin

    How to make a website

    7:33 pm on January 21st, 2010 Reply
    • Hello Alvin,
      I used WordPress to set up this website. It is free, open source, and also supported by most web hosting sites.

      There are also many free and very useful WordPress themes and plugins out there, so I really like the WordPress platform.

      However, choosing the right website-platform depends on what your online goals are, and how you want to allocate your time (e.g. design vs. programming vs. content).

      8:43 am on January 22nd, 2010 Reply
  3. asas

    12:36 am on January 14th, 2010 Reply
    • Hello Asas,
      If you want to test out a Spry comment box, visit this test static page -
      http://shibashake.com/test-table.html

      This current page is just a regular WordPress page.

      Also enter some regular text – e.g. Asas is testing the Spry page. Otherwise, the Spam filter may catch it and not publish the comment.

      4:26 pm on January 21st, 2010 Reply
  4. Hello Dule,
    Check out –
    http://www.shibashake.com/wordpress-theme/display-wordpress-comments-in-a-static-web-page

    That has all the PHP code I used to get comments from WordPress and generate an XML stream.

    12:22 pm on August 25th, 2009 Reply
  5. Hello Andy,
    If you follow the steps above, you will be able to create your own comment box – for free :) You can also visit my website and look at the source code of the one that I have created –
    http://shibashake.com/test-table.html

    It is tied to a WordPress backend, and I created it using the steps I laid out above. Let me know if you have any questions.

    12:17 pm on July 22nd, 2009 Reply
  6. Logan

    Very useful! Appreciate the excellent tutorial.

    12:23 pm on August 25th, 2009 Reply
  7. Dule

    Hey there, I know how to make the Spry part, but the part where I have to connect Spry to WordPress is kinda blurry to me :)

    12:21 pm on August 25th, 2009 Reply
  8. Asanka Ranjeewa

    I need complete comment box cording

    12:19 pm on August 18th, 2009 Reply
  9. andy cheng

    how to create comments box and i’ll pay you

    12:16 pm on July 22nd, 2009 Reply
  10. What great info! Thanks so much, Shibashake

    12:15 pm on July 22nd, 2009 Reply
  11. Shibashake, thank you for writing this as I find hubs to be very clear, concise and highly informative. Again, I will bookmark this hub because I will utilize your technique.

    12:14 pm on July 22nd, 2009 Reply
    • ShibaShake

      Thanks dohn. Glad you found it to be useful. Let me know if you have any questions.

      11:35 am on October 9th, 2009 Reply

RSS feed for comments on this post. TrackBack URL

Leave a Reply

search button search button
rss