Comment Contest Code and FAQ

Since we launched the comment contest we have been somewhat bombarded with questions. Everything from how it works to if we can share the “plugin” we used for it.

First off its not a plugin its some code I hacked together. I think by answering some of these it will also explain why the code below does what it does. Its really all in the sql querry.

What post do I have to comment on to win daily?

The winner is randomly selected from comments made on that day at 23:55.

If I spam all past posts with comments does that help?

No it has to be from a comment on the days post.

If you don’t post anything today can I win by commenting on previous ones?

No its only on that day.

So again when a post is made you come in and comment you are entered to win. Hopefully that is straight forward enough.

Now the biggest question was what plugin we used to make it happen. Again its not a plugin. To make the random selection happen many things had to come into play and lots of cross table selects. I am sure this code is far from perfect as I, yes me, did it. With my lack of programing ability this took me a long time to figure out.

It was difficult for me because of all the factors in play:

  • It had to select a random commentator.
  • It had to select the commentator from a post made that day.
  • it had to select from ONLY posts that were published (I made lots of drafts).
  • It had to select from only posts and NOT pages.
  • It had to select from the proper wordpress post type (we have custom post types for stuff like marketplace and what not).
  • It had to select uniq from that day.
  • IT had to have a user with an email (so we can contact them).
  • It had to have a user that had an approved comment.
  • It had to not have me as a eligible winner.

I also want to automatically:

  • Email the winner.
  • Email the person on my staff who will ship these out.
  • BCC email myself so I know its working nightly.
  • Put a unique code in the subject line so we can properly track it and authenticate it.

So here is the code that makes all that happen:

< ?
$db_host = "localhost";
$db_user = "dbusername";
$db_passwd = "password";
$db_db = "database";
$result=mysql_query("select comment_author,comment_author_email,comment_author_url,comment_content, comment_date from wp2_comments where comment_author_email NOT IN (SELECT email FROM commentcontest) AND comment_author !='shoemoney' AND comment_approved=1 AND comment_post_id =(select id from wp2_posts where post_status ='publish' AND post_type = 'post' AND  post_date like '$today%' order by post_date limit 1) order by rand() limit 1");
while($row = mysql_fetch_object($result))
                $author = $row->comment_author;
                   $email = $row->comment_author_email;
                   $url = $row->comment_author_url;
                   $content = $row->comment_content;
                   $comment_date = $row->comment_date;
if (!$email=='') {
$code =md5($email);
$subject = "$today ShoeMoney Comment Contest Winner $code";
        $headers = "Return-Path:\r\n";
        $headers .= "From: Anna \r\n";
        $headers .= 'Bcc: Jeremy ' . "\r\n";
$message = "Dear $author,
Congratulations!  You are the randomly selected winner for your comments written on a blog post on $today. You have won a ShoeMoney t-Shirt and a chance at this months grand prize! \nTo Claim your t-shirt Please reply to this email with your shirt size, style of shirt (male or female Small to XXXL),  and your mailing address.\n We will ship them to you free of charge anywhere in the world!  Please respond to this email within 48 hours with your address if you wish to claim your free shirt!\nCongratulations and good luck at winning this months grand prize.  For more information check
Please note - We ship out shirts on the first and 15th of each month. \n
Also please keep the subject line with the code intact so we can verify its authenticity!i\n
Thanks for your contribution to the ShoeMoney Blog!  We appreciate your support and input!\n
Jeremy Schoemaker AKA ShoeMoney";
mysql_query("INSERT INTO commentcontest(doc,author,email,url,content) VALUES('$today','$author','$email','$url','$content')")  or die(mysql_error());
print "$author $email $url $content $comment_date";
} else {
        die("no data\n\n");

Then for the actual page that shows the winners I select from that from the winners to show on the comment contest results page:

< ?php
$db_host = "localhost";
$db_user = "dbusername";
$db_passwd = "password";
$db_db = "db";
$contestdate=date('F Y');

< ?php $result=mysql_query(“select * from commentcontest where dtime > ‘2008-12-01 00:00:00’ order by dtime DESC”); while($row = mysql_fetch_object($result)) { $doc=$row->doc; $author = $row->author; $url = $row->url; $content = $row->content; $comment_date = $row->comment_date; print “”; } echo ”

Date Winner Name Comment
$doc $author $content

. So again its not a plugin and with the code above if you want to do it, it should save you a lot of time whether you do it yourself or hire some monkey off of Odesk.

BTW if your going to hate on my code at least offer suggestions on how to improve it =P If someone wants to turn this into a plugin go ahead.

About The Author

Comments 19

  1. Geiger
    • Jeremy
    • Dr Jeremy Schoemaker
  2. Ramon
    • Dr Jeremy Schoemaker
  3. Kevin Thomas
  4. Chris
  5. Georgie
    • George
  6. d3so
    • Jeremy
  7. Ron
    • Jeremy
  8. John Caskoby
  9. sukosaki @ sukosaki
  10. fas
  11. Thomas Vanhoutte