Textpattern Comments Tutorial

When I set out to redesign wilshire|one, the first area I concentrated on was the display and entry of comments. With some recent changes in RC3, several plugins have been introduced that allow you to customize the display of comments with no code hacking involved. I’ll attempt to run through my entire comment system setup detailing all of the pieces and parts including the plugins I used and code examples.

And let me put this out there up front, this may not be the only way, or even the right way to accomplish this. I prefer to call it the “it worked so I stopped playing around with it” way. If you know of a better way to accomplish this, by all means, chime in.

Site Preferences

Comments as a numbered list admin preference The first thing you’ll want to do is make sure that comments are not displayed as a numbered list. Check the admin -> preferences tab to confirm that this is set correctly.

Plugins

Your next step is to download a handful of plugins that will be used to create both the comments listing and the comment entry form.

The Forms

Textpattern comes with 2 standard forms called comments and comment_form with the first being your comment list form and the latter being your entry form. These forms cannot be removed so be sure to copy the contents of the forms and save them off as a backup before editing them.

Comment Entry

I had 2 main goals in redoing the standard comment form. The first was to change the table based layout and the second was to present new and returning visitors a different comment entry form since a returning user only needs to enter their comment as their name, email and web address are stored in a cookie. In removing the table I wanted to use <label> tags for my input fields labels but this causes some validation problem since Textpattern does not give the comment entry fields IDs. I ended up replacing some of the standard Textpattern tags with a combination of HTML and PHP to get where I wanted. Here’s the form.

  1. <div id="commentform">
  2. <h3>Add Your Comment</h3>
  3. <p>Comment form disclaimer. Or not.</p>
  4. <txp:if_cookie>
  5. <strong>Welcome back, <?php echo pcs('name'); ?>.</strong>
  6. <input type="hidden" id="name" name="name" value="<?php echo pcs('name'); ?>" size="25" tabindex="1" />
  7. <input type="hidden" id="email" name="email" value="<?php echo pcs('email'); ?>" size="25" tabindex="2" />
  8. <input type="hidden" id="web" name="web" value="<?php echo pcs('web'); ?>" size="25" tabindex="3" />
  9. </txp:if_cookie>
  10. <txp:if_no_cookie>
  11. <span><label for="name">Your Name:</label><input type="text" id="name" name="name" value="<?php echo pcs('name'); ?>" size="25" tabindex="1" /></span>
  12. <span><label for="email">Your Email:</label><input type="text" id="email" name="email" value="<?php echo pcs('email'); ?>" size="25" tabindex="2" /></span>
  13. <span><label for="web">Your http://</label><input type="text" id="web" name="web" value="<?php echo pcs('web'); ?>" size="25" tabindex="3" /></span>
  14. </txp:if_no_cookie>
  15. <span><label for="message">Your Comment:</label><textarea id="message" name="message" cols="30" rows="12" style="width:300px;height:250px" tabindex="4"><?php echo pcs('message'); ?></textarea></span>
  16. <span><txp:comment_remember /></span>
  17. <txp:comment_preview />
  18. <txp:comment_submit />
  19. </div>
  20. Download this code: /code/commentform.txt

So what do we have here? It don’t look too pretty, but you can see the result on the comment form below. When a user has a cookie set, the only thing that will be displayed is “Welcome back…” Their name, email and web address will be saved in hidden fields. The pcs() function will grab those values from the cookie.

If the user does not have a cookie set, the standard form will all fields will be shown. I replace the Textpattern tag for the comment input fields on the form in order to add an id=”” to match my label for=”” tags. This is necessary if you want your page to validate. And thats it. All thats left for you to do is style your form appropriately.

Comment Listing

For my comments listing I wanted to implement many of the same things originally described in Jon Hicks’ well known Comments Hacks. Alternating classes for comments, a special class for the site owner, a div instead of list based output, comment numbering and the user of gravatars are all taken care of in the following example. Five plugins are used in this one form to get the job done, but in the end your source code stays intact. Now on to the form.

  1. <div class="comment <txp:ptv_condition><txp:ptv_if plugin="ajw_if_comment_owner" web="www.yoursite.com">mycomment</txp:ptv_if> <txp:ptv_else><txp:ajw_comment_alt /></txp:ptv_else> </txp:ptv_condition>">
  2. <txp:glx_gravatar />
  3. <txp:comment_permlink><txp:ajw_comment_num /> </txp:comment_permlink>
  4. <txp:comment_name /> - <txp:comment_time />
  5. <txp:message />
  6. </div>
  7. Download this code: /code/commentlist.txt

A nice batch of tag soup, but quite effective. I’ll break down the form to get into more detail.

The first line is where the bulk of the work is done. I’ve assigned a class called “comment” to each comment so that I can assign styles that will be common to all comments, whether by myself, others, odd numbered or even. After the “comment” class is assigned, the ptv_if plugin is employed to do some conditional testing. If the comment is my own, it receives an additional class of “mycomment”. Otherwise, the comment will receive the appropriate class depending on whether its an odd or even numbered comment.

In the end, this will produce one common style for all comments, alternating styles for every comment, and a special style for your own comments. Depending on what you’re trying to do this might be overkill but my use of Nifty Corners made it necessary for me.

The rest of the form is a bit simpler. The first line here displays the users gravatar if applicable. The second line will create a permlink on the comment number, rather than the standard # sign. The third line will display the name of the commenter and the time the comment was posted.

The final lines display the actual comment and close the div element. And thats it! All that’s left again is adding some style to your comments. It might be necessary for you to add some extra block level elements to get the look you’re going for but this should give you the general idea.

Wrapping it Up

I spent a considerable amount of time getting this to work exactly how I wanted so in all likelihood you’ll need to do some tweaking here and there. But hopefully this will give you a good start in the right direction. Enjoy and happy commenting.

Comments

3. May 2005

ah, spilling your secrets. good.

but you meant to tell me there’s not a single rss_plugin used towards comments? i ask you, is that reasonable?

5. May 2005

It’s a good post.

7. May 2005

Thanks for sharing Rob~

This saved a ton of tinkering for me!

Jamie

8. May 2005

Thanks for this great Article!

BlueMaex

9. May 2005

Once again an opportunity to learn something new,
I wouldn’t have known sometimes,what I should have done,if you wouldn’t be around to help.
Thanks for everything

regards, marios

marios

10. May 2005

Actually you can wrap the form field in the label tag and it’ll validate. Saves you from having to use your own tags instead of the TXP tags, which has caused problems on some sites. Thanks for the other info, though.

26. May 2005

This looks absolutely brilliant. Thanks.

7. June 2005

Thanks. This is very helpful…but I am still wishing there was a plug-in that would allow me to easily add multiple Your Comment Fields (not named ‘your comment’, of course :)

11. June 2005

Rob, awesome documentation. Thanks. The only thing I would add to this is the glx_thiscomment plugin, which can be found here .

That plugin will output the $discussid of the current comment if you’re using div’s for comment display.

mike

22. June 2005

Newbie question here, my advanced apologies…

How do I add these to a page/article?

I’ve made the forms, installed and activated the plugins but I have no idea how to actually use them.

Help would be very helpful, I really want to get this to work it looks very professional!

les

14. July 2005

Thanks Rob… It took me 15 minutes to build it up with your explanations. Great tutorial.

19. July 2005

Thanks heaps for this Rob – had it working really well, but I think the system is broken by the newer SVN builds (which I know you are avoiding!).
Does this thread impact on your comments system?

9. August 2005

Great tutorial. Nice to have someone spell some of this stuff out for those of us who feel like we are learning spanish as 90 year olds. Anyhow, I am with les, in that I am not real sure how to apply the comments forms to my pages. I am assuming that I may need to actually go into the textpattern directory and replace the default forms?

Thanks.

21. September 2005

Just trying to get your plugin to work on my archives page. Just using the basic tag:

I get the following when displaying my page in the browser:

Notice: Undefined variable: txp_current_plugin in /home/twinfish/public_html/journal/textpattern/lib/txplib_misc.php on line 398

A problem occured while loading the plugin: -> Notice: Undefined variable: count

I installed the Zem debug plugin to see if that would help but it just added the second part of the message ‘A problem…’

Am using Textpattern version: 1.0rc5 (r705)

Any help would be great – thanks!

Tim Barry

21. September 2005

Sorted! By changing the publisher status to ‘live’ the messages go away. You said that somewhere on this site – cheers!

Tim Barry

28. September 2005

This looks great, but for some reason my textpattern 4.0 installation isn’t playing nicely. The php code is not parsing and instead is showing raw in the form values, if this makes sense. So the form looks like

Your Name:
Your E-mail:

And so on. Any ideas as to what is going wrong?

Will

28. September 2005

OK, that didn’t quite work as the code got lost in translation. There’s a screenshot of what I mean at http://www.thinkbuddha.org/comments.jpg

Thanks.

Will

12. October 2005

Any chance this tutorial will be updated for the latest edition of TextPattern?

17. October 2005

I’m having the same trouble as Will on a client website…

Any advice?

2. November 2005

Amazing, thanks alot! :-)

30. November 2005

Great tutorial, easy to read and understandable.

I have one question though: I recently switched to Firefox and now I realize that the name, email and http fields are ‘indented’ whereas IE shows them correct. Do you have any idea where I should adjust that?

thank you in advance.

Commenting is closed for this article.

Textpattern Solutions

Textpattern Solutions

Textpattern Solutions is the first book published on Textpattern.

details at friends of ED or the official book website.

buy it at amazon.com