WordPress welcome back script
September 9th, 2008
I was thinking the other day for those of us who do not allow, or want to allow registering on our blogs, how we could still offer somewhat of a “personalized” greeting when a visitor returns. We also don’t want to leave our first time visitors out of the loop either. I just threw this together real quick, but the possibilities are endless…
Making use of WordPress’ comment_author cookie check
So right off the bat WordPress will by default, set a cookie with the information from your comment form when a user leaves a comment. You may have noticed that when you go to other sites where you left a comment before, this data is already populated for you. Some times though it is nice to leave the option of NOT saving the user’s information because maybe they are on a public computer or perhaps they just do not want it saved for an array of reasons. In this case, we can not get so personal.
The “meat” and “potatoes”
<p> <?php
if(isset($_COOKIE['comment_author_'.COOKIEHASH])) {
$lastCommenter = $_COOKIE['comment_author_'.COOKIEHASH];
echo "Welcome Back ". $lastCommenter ."!";
}
else {
echo "Welcome, Guest!";
}
?>
</p>
Basically what we are doing here is checking to see if the “comment_author” cookie is set. If it is, this means or possibly could mean, they have left a comment here before. It also could mean they are on a public computer with someone else’s information being displayed.
I will add on to this script later to drop the cookie or change the author name if it is not them, but for right now, this is kind of fun.
You could do a lot of things with this:
- Display a welcome message
- Tell first time visitors about your more popular posts
- Show them an RSS feed subscription link
but you don’t want to overdue it because:
- The user might not have cookies enabled
- They may frequent your site, but do not wish to leave comments
- It might not be their computer
Obliviously there are a lot of factors to weigh out here, but if you want to mess around with this, go for it.
- Posted at 9:37 am in tutorials
- Leave a comment