Welcome back, Dude!

It’s always nice to be recognized. I tend to return to the same place a lot if I like it — my favorite sushi and Thai place, my favorite coffee houses, etc. — and they recognize me and greet me. That’s pleasant. It’s similarly nice to be “recognized” online. Not equally nice, but similarly. This is why people do Gravatars, this is why we don’t post anonymously: if people recognize you on sight it makes you ever so slightly happier.

Michael has had a nifty feature for a while that is subtle but nice. If you’ve posted a comment on his site, instead of the typical form inputs for your name, email, and URL it welcomes you back personally.

“Welcome back, ####.”

That’s a friendly sentiment, it really is. I just thought it was some mysterious feature and then I realized last night that not only wasn’t it mysterious but it was probably damned simple. Effective but simple. So I set about duplicating the feature. Here’s how it’s done.

In essence, if the commenter has the appropriate cookie, WordPress in its infinite wisdom populates some variables for us in the page. Those variables are $comment_author, $comment_author_email, and $comment_author_url. All we really need to do to determine if we “know” the commenter is to look for a non-empty $comment_author. If non-empty, greet her however you wish! Here’s what I did:

[code lang="php"]

<

p>Welcome back (Change)

[/code]

Now, the only other cutesie thing we need is a bit of JavaScript. Michael’s is clean and elegant, so we’ll glom his hopefully without causing ire. Let’s see that code:

[code lang="javascript"]function ShowUtils() { document.getElementById("authorinfo").style.display = ""; document.getElementById("showinfo").style.display = "none"; document.getElementById("hideinfo").style.display = ""; } function HideUtils() { document.getElementById("authorinfo").style.display = "none"; document.getElementById("showinfo").style.display = ""; document.getElementById("hideinfo").style.display = "none"; }[/code]

That can be stored directly in your template, but you’d probably be better served creating a file — I put mine in the template directory in a special “scripts” subdirectory — and including it in the header.

That’s all there is to it. Now your visitors should feel just that slight amount more special :) .

January 14, 2005 • Posted in: Design

25 Responses to “Welcome back, Dude!”

  1. 1

    ColdForged (972 comments) - January 14th, 2005

    And I apologize once again for the poor formating. Sigh.

  2. 2

    Mike (41 comments) - January 14th, 2005

    Hope ya don’t mind if I lift that little bit of code myself. ;) Nicely done. Btw, for some reason I keep triggering anti-spam filters on your blog now.

  3. 3

    ColdForged (972 comments) - January 14th, 2005

    Lift! Lift!

    Btw, for some reason I keep triggering anti-spam filters on your blog now.

    Yeah, I noticed that. Hopefully I’ve corrected the problems behind it. Although, of course, I just noticed you had another one in there. Officially confused!

  4. 4

    coldforged.org » Live comment previews - January 20th, 2005

    ake my regular commenters feel a bit more at home—like the recently mentioned “welcome back” code—was reuse some cookie data to personalize the preview [...]

  5. 5

    coldforged.org » Blog Archive » Live comment previews - February 28th, 2005

    [...] ake my regular commenters feel a bit more at home—like the recently mentioned “welcome back” code—was reuse some cookie data to personalize the preview [...]

  6. 6

    mark (32 comments) - May 15th, 2005

    How do you then get the personalised message to appear on other pages? – like I notice you’ve done. I’ve fiddled about a lot but can’t figure it out. Please help! :)

  7. 7

    ColdForged (972 comments) - May 15th, 2005

    This is the relevant code you’re looking for:

    [code lang="php"]if( !isset($comment_author) ) { $comment_author = isset($COOKIE['comment_author'.COOKIEHASH]) ? trim(stripslashes($COOKIE['comment_author'.COOKIEHASH])) : ''; }[/code]

  8. 8

    mark (32 comments) - May 16th, 2005

    Ah great! Thankyouverymuch! :)

  9. 9

    Christopher (8 comments) - May 21st, 2005

    I’ve had a problem with getting it to work. It welcome them back but then makes them anom when they post.. any ideas of what i might be doing… wrong… so basically it welcomes them back but if they are not logged in it won’t put the variables in… i’m going to go through yoru code again i’m just kind of stumped!

  10. 10

    Gary (5 comments) - May 22nd, 2005

    Does the ‘relevant code’ you mention in Comment 6 have to be included for sidebar or just on ‘pages’ that don’t use the regular templates?

    Also, while I am asking
    - what code are you using for your ‘since last visit’ under the Welcome in the Sidebar - what code are you using for the little ‘new’ icons after each comment?

    Thanks!

  11. 11

    Christopher (8 comments) - May 22nd, 2005

    Mine is not populating the actual Name: Email: or URL Field when I run it.. it will work if I’m logged in.. and it will welcom people back but it will not actually remember then when they post a comment. Do I need to do anything to the Name, email, and url fields…?

  12. 12

    Christopher (8 comments) - May 22nd, 2005

    What I’m talking about is this part of the code:

    <fieldset> <label for="author">Name<input type="text" name="author" class="commentInput" id="author" value="" size="12" tabindex="1" /></label> <label for="email">Mail (will not be published)<input type="text" name="email" class="commentInput" id="email" value="" size="20" tabindex="2" /></label> <label for="url">Website<input type="text" name="url" class="commentInput"id="url" value="" size="12" tabindex="3" /></label> </fieldset>

  13. 13

    Christopher (8 comments) - May 22nd, 2005

    hmm.. sorry didn’t mean to post a form… like that..

  14. 14

    ColdForged (972 comments) - May 23rd, 2005

    Gary asked: Does the ‘relevant code’ you mention in Comment 6 have to be included for sidebar or just on ‘pages’ that don’t use the regular templates?

    Either one, really. It checks to see if $comment_author is already set so it does no harm in the case of it already being set.

    Also, while I am asking – what code are you using for your ’since last visit’ under the Welcome in the Sidebar – what code are you using for the little ‘new’ icons after each comment?

    It’s “WP Since Last Visit” available here.

    Christopher said: Mine is not populating the actual Name: Email: or URL Field when I run it..

    Take a look at my comment template and search for the code that says value="<?php echo $comment_author; ?>". In each of your input fields you need to echo the appropriate variable like that one.

  15. 15

    Christopher (8 comments) - May 23rd, 2005

    CF – Thanks for the tip.. that was seriously cool of you to share your comments template so I could figure out what I was doing wrong.. mad props.

  16. 16

    ColdForged (972 comments) - May 23rd, 2005

    Christopher said: CF – Thanks for the tip.. that was seriously cool of you to share your comments template so I could figure out what I was doing wrong.. mad props.

    Glad it helped! You can actually see any of my templates by following the directions in this comment, though I don’t necessarily keep the viewable ones 100% up to date. They’re close enough for government work, though :) .

  17. 17

    Angsuman Chakraborty (3 comments) - May 24th, 2005

    Cool idea! Now I am thinking where can I fit in my already cramped for space blog :) Today I downloaded some of your plugins too. Thanks dude.

  18. 18

    Dogberry Patch - May 26th, 2005

    Welcome Back, Dude!

    Status: Installed

    http://www.coldforged.org/archives/2005/01/14/welcome-back-dude/

    It’s always nice to be recognized. I tend to return to the same place a lot if I like it — my favorite sushi and Thai place, my favorite coffee houses, etc. — …

  19. 19

    Chris Bunting (4 comments) - July 15th, 2005

    Thank you for posting this quick tutorial and sharing the code for implementing this nifty little hack! I’m actually having some trouble with it working properly on my site, and was hoping you wouldn’t mind throwing out some possible suggestions so that I can fix it.

    I’ve added the code exactly as you specified, and it seems to function properly, except the `if( ” !== $comment_author )` statement to check for a blank `$comment_author` seems to be evaluating as true even when the author field is blank. Therefore, the “Welcome back” message is displayed, with the authorinfo div hidden, when it should be visible to let users know the info needs to be entered.

    I hope this makes sense, or if you can, check my site to see what I mean.

    Thanks for your time!

  20. 20

    ColdForged (972 comments) - July 15th, 2005

    Chris said: Therefore, the “Welcome back” message is displayed, with the authorinfo div hidden, when it should be visible to let users know the info needs to be entered.

    Can you post your template here and post a link to it back here? You can also take a gander here at my template to see if anything jumps out at you.

  21. 21

    Chris Bunting (4 comments) - July 15th, 2005

    Maybe I should have mentioned it, but I’m using the Paged Comments plugin, so I added the code to wp-paged-comments.php. You can see the code here. Also, I made sure to peruse your source before asking for help, just to make sure I wasn’t missing something obvious (which is usually the case). :)

    Thanks again.

  22. 22

    ColdForged (972 comments) - July 15th, 2005

    Ah. That might make the difference. Try my version of your code and see if it helps.

  23. 23

    Chris Bunting (4 comments) - July 15th, 2005

    Thank you sir! I truly appreciate you taking the time to help me get this squared away. It works beautifully.

  24. 24

    ColdForged (972 comments) - July 15th, 2005

    Great, glad you’re up and running.

  25. 25

    JOSEPH (14 comments) - December 15th, 2005

    Hey i know this is a little old but it still seems pretty useful so i’m trying to employ it into a few of my own Themes. I can fairly well juggle where the line go that need added to the comments.php (* i’m not totally php illiterate), but what’s up with that last bit of code you posted that suppose to dress things up. I don’t see any mention of it to be added to the style.css file and surely you can’t expect someone to add that clump into a php file raw like that. I,m just trying to get some better clarification for myself and also others who are going into these matters for the first time.

    Thanks: