WordPress: Thank that first time commentator!

Thumbnail credit: Premshree Pillai

To a website, comments are important - this you must agree. But not all visitors leave comments - in fact, very, very few. Most of them care about the content only, and tend to leave (bounce) the site right after getting the information they need (so sad a life, huh?)

Many tips have been introduced and used to encourage visitors to contribute to your site via comments. To my knowledge, and to name a few:

  • Use dofollow links in comments. By default, WordPress and other blogging systems mark links in comments with rel="nofollow" attribute. This attribute tells search engines to not follow the links, which means the commenter’s site will not be able to share any Google juice with you. While effective in fighting spammers, this technique may a bit disappoint the real visitors. Plugins like Dofollow address this problem and remove “nofollow” attribute from comment links.
  • Further promote the commenter’s blog (if any). CommentLuv is a plugin that “will visit the site of the comment author while they type their comment and retrieve a selection of their last blog posts, tweets or digg submissions which they can choose one from to include at the bottom of their comment when they click submit”.
  • Choose a (random) comment to give small prizes such as free ebooks, preminum themes etc.
  • Explicitly ask the readers to give comments at the end of the article - “Please share your thoughts”, “What do you think?”, “What say you?” etc. etc.

Today I would like to mention another method to encourage commenting. Though this won’t likely attract more commenters, it may encourage existing ones to leave more comments and become more effective contributors.

The method is called “Thank first time commenters” and it works like this:

Normally when a visitor leaves a comment for the first time on your blog, the comment is displayed under “awaiting moderation” status and visible to him only. When there’s nothing wrong with this approach, it’s not really interesting for the commenter himself. Now how about showing him a “thank you” or “welcome” message, or better, a whole page, for the first time contributing his [great] ideas to your site? This time it’s much cooler!

Let’s turn the idea into real action for a WordPress site.

Step 1. Detect first time commenter

Comments
Original image from Chrismar

To detect whether the comment author is leaving his first comment on you site, we need to hook into one of the comment-related functions (you can read about hooks here). For this purpose I use comment_post(), but there may be some other alternatives which I don’t know about comment_post_redirect filter. This filter is applied just before WordPress redirects the commentator after him posting a comment - exactly what we need.

In the theme’s functions.php page, add these lines:

add_filter('comment_post_redirect', 'check_first_time_comment');
/*
 * This functions accepts one parameter being the location to redirect commentators to
 */
function check_first_time_comment($location)
{
    global $comment; // get the comment from global variables
    // if it's not "unapproved", don't modify the redirect location
    if (0 != $comment->comment_approved) return $location;
    // now check if it's really the first time the commenter comments
    // to do this, we check the number of comments this author have left on our site
    global $wpdb;
    $sql = "SELECT COUNT(comment_ID) from {$wpdb->prefix}comments WHERE comment_author_email='{$comment->comment_author_email}'";
    if ($wpdb->get_var($sql) > 1) return $location; // he's left more than 1 comment - do nothing with him
    //--- if we reach here, this is the first comment he leaves on our site. Do our stuffs now! ---//
    // first save the comment data into session. We'll use it later
    if (!session_id()) session_start();
    $_SESSION['first_time_comment'] = serialize($comment);
    // now modify the $location so that WordPress redirects this commentator to our special page
    return '/thank-you-for-contribution/';
}

What the code does is pretty self-descriptive. The function check_first_time_comment($location) is triggered just before WordPress redirects the commentator to a location (being the post, the page, or the error page). It takes one parameter, being the location url. Upon triggered, it checks for the status of the comment and only does its deeds if the comment is “unapproved” (status code 0). Then, it does a further check to see if this is really the first time the comment author leaves a comment on the site with the email address. If this is true, it saves the comment data into session for later retrieval, and modifies the location string to redirect the commenter to the thank you page.

Step 2. Create a “Thank you” landing page

Thank you!
Original image by Premshree Pillai

This is the page where our valuable commenter is redirected to. To create such a page, first copy page.php in the theme folder into thank-you-page.php. Open it and locate these lines on the top:

<?php
/*
Template Name: Page
*/
?>

Change the template name to something descriptive like “Thank You Page”, and add some logic, like this:

<?php
/*
Template Name: Thank You Page
*/
if (!session_id()) session_start();
if (!isset($_SESSION['first_time_comment']))
{
    // the page should not be directly accessed
    header('Location: /');
    exit();
}
// now get the comment data from session and utilize it
$first_time_comment = unserialize($_SESSION['first_time_comment']);
// clear the session
unset(unserialize($_SESSION['first_time_comment']);
$the_post = get_post($first_time_comment->comment_post_ID); // get the post of the comment
if (empty($the_post))
{
    // for some reason, we cannot find the post.
    // The action here depends on you. For me I redirect to home.
    header('Location: /');
    exit();
}
// tell the browser not to cache this page in anyway
// !IMPORTANT: You may also want to exclude this page from cache plugins too!
header("Cache-Control: no-cache, must-revalidate");
/*
"Thank you" content goes here
With the post on hand, you can enrich this thank you page with:
- similar posts
- posts in the same categories
- random posts
- most read posts
- most commented posts etc.
*/
?>

Save the template. Now go to WordPress control panel, create a new page with “Thank You Page” as the template, and thank-you-for-contribution as the slug. You may want to disable ping and comments for the page too.

There! Your blog is now ready to welcome the first time commentators!

But wait… there’s still a catch.

Step 3. Post-production

Now, if you list down your pages somewhere on your blog, “Thank You” page will stupidly appear. The fix is easy: exclude the page ID. Also, if you have a cache plugin installed, it’s best to exclude Thank You page from caching.

Want to see it in action? Just leave a comment here as a new commentator!

  • Wow, it’s a cool trick. Let me test ;)

  • Thanks for sharing this technique, I’m going to implement it on my site.

  • Let’s see :)

  • Let’s see how it works. I’m a first time commentator.
    Thanks.

  • Interesting!
    Will see how it works. Thanks. :D

  • That sure was great!!
    will try it for sure…
    Can you shed some lights in how DB intensive it could be.
    I had problem with ‘Related Post’ plugin — it was too heavy for the db server.

  • Xnepali, it’s no intensive at all, just a simple query. If you’re worried too much about DB impact, try DB Cache Reloaded plugin.

  • Wow, you’ve inspired me to leave my second comment.  I was so impressed by the “thank you” after I left my first comment, I wanted to see how you had accomplished this.   Glad you’re showing the process.
    You hit the nail on the head …  Somehow, there it is a bit of a let-down feeling when you leave a comment at a site, only to see that it is going into moderation, and not getting posted immediately.  (Of course, that’s necessary and we do it on our own blogs, too.)  But still, it doesn’t give you that warm-and-fuzzy feeling I got when you popped up that Thank You page!
    Great stuff :)
     

  • Neat.
    Just about to press the “Submit” button…

  • it works ok

  • nice little trick

  • Thanks… Nevertheless one much more outstanding admittance, it is truly the reason all of came back to the actual website time and again!

  • Great tip, I hope it works

  • Let’s see how it works, then.

  • cool trick, bro

  • I like that a lot. Your comment ‘Thank You’ was unexpected and welcome surprise. And the links through to here and elsewhere clearly pull people through. Will give it a try.

  • Testing to see how the page works.

You can follow any responses to this entry through the RSS 2.0 feed.