Textpattern tips, tutorials and code snippets

Personal greeting in comments form

Its nice to greet your visitors with a personal message when they revisit your site. The following is a simple method I use to place a greeting in the comments form.

If the visitor has commented before and their info is “remembered”, then in this example their name will be displayed.

Open your comment_form

Welcome back <txp:php>echo pcs('name'); </txp:php>

Enter the above into your comment_form where you wish the greeting to be displayed. Edit as you see fit.

4 Comments Comment feed

To prevent “Welcome back” from appearing for those who haven’t commented, use the following:

<txp:php>
$u = pcs('name');
if (!empty($u))
    echo 'Welcome back, ', pcs('name');
</txp:php>
  • Sander
  • 9 September 2012

And if I want to replace any other text with this – is there any “else” option?

  • etc
  • 9 September 2012

You should at least pass pcs('name') through htmlspecialchars. If in doubt, it all can be done with etc_query plugin:

<txp:etc_query globals="_COOKIE,_POST" data="{?name}">
  Welcome back, {?}.
<txp:else />
  Other text.
</txp:etc_query>
  • etc
  • 9 September 2012

Sorry, it should be

<txp:etc_query globals="_COOKIE,_POST" data="{?txp_name,name}">
  Welcome back, {?}.
<txp:else />
  Other text.
</txp:etc_query>

Add a comment

Use Textile help to style your comments