Turn your commenters into subscribers!

January 31st, 2011 – 64 Comments

Only a small percentage of your visitors leave a comment. This group of people is probably also interested in subscribing to your newsletter, if you have one. So I created a method to add a “subscribe to my WordPress newsletter” checkbox to my comment form a couple of weeks ago. The result? 40 new subscribers in less than 2 weeks. That’s over 1,000 new subscribers each year if it continues to grow in this fashion.

This isn’t for the faint of heart, if you’re not a programmer this might be a bit tough to do, but I’ll outline how it works:

Adding a comment meta value

Using the reasonably new comment_meta API, we’re gonna add a piece of data to comments, that stores whether people want to subscribe to the newsletter or not. I did that by adding the following function to my functions.php file:

function ymc_add_meta_settings($comment_id) {
  add_comment_meta(
    $comment_id, 
    'mailchimp_subscribe', 
    $_POST['mailchimp_subscribe'], 
    true
  );
}
add_action ('comment_post', 'ymc_add_meta_settings', 1);

Add a checkbox to the comment form

That wasn’t too hard yet right? Next we add the checkbox to the comment form, wherever that is located in your theme, it’s just plain (X)HTML:

<input type="checkbox" name="mailchimp_subscribe" 
  id="mailchimp_subscribe"/> 
<label for="mailchimp_subscribe">
  Subscribe to Yoast WordPress Newsletter (weekly email)
</label>

Subscribe the commenter

That’s not really rocket science either. The next step is to take this data and work it. What we’ll do is use the Mailchimp API to automatically subscribe people, and send them a confirmation email (we still need to make sure they double-opt-in).

First, let’s grab the comment:

function ymc_subscription_add( $cid, $comment ) {
  $cid = (int) $cid;
	
  if ( !is_object($comment) )
    $comment = get_comment($cid);
		
  if ( $comment->comment_karma == 0 ) {
    // DO WORK
  }
}
add_action('comment_approved_','ymc_subscription_add',10,1);
add_action('comment_post', 'ymc_subscription_add', 60,1);

The code above makes sure we only do anything with comments that have been approved. So if a comment is approved as spam, it gets ignored. It also makes sure the $comment variable is filled with the comment. Now once we’ve done this, we’re going to check whether the commenter wanted to be subscribed to the newsletter, so within the DO WORK section above, we add this:

$subscribe = get_comment_meta($cid, 'mailchimp_subscribe', true);
if ( $subscribe == 'on' ) {
  // DO WORK
}

We’re checking for “on” because that’s how PHP stores a checked checkbox, and this way we can reuse the $subscribe variable later. Once we’re sure the commenter wanted to be subscribed, let’s get all the data we need to add a subscriber to our mailing list. To do that, we’ll need to know:

Once you’ve got these three, we can create the post request:

$apikey   = ' <your API key> ';
$listid   = ' <List ID> ';
$endpoint   = 'http://<your endpoint>.api.mailchimp.com/1.3/?output=php';

$request   = array(
  'apikey' => $apikey,
  'id' => $listid,
  'email_address' => strtolower( $comment->comment_author_email ),
  'double_optin' => true,
  'merge_vars' => array(
    ' <merge tag for name> ' => $comment->comment_author,
    'OPTIN_IP' => $comment->comment_author_IP,
  )
);
$result = wp_remote_post(
  $endpoint.'&method=listSubscribe', 
  array( 'body' => json_encode($request) ) 
);

Note: setting double_option to false would forego the sending of an opt-in email, thereby immediately subscribing people to the list. Do not do this as it is illegal in most parts of Europe and in the US to do so.

As you can see, we’re using the WP_HTTP wrapper function wp_remote_post to handle the post. At this time I’m not actually doing anything with the result, because I’m a sloppy coder. You should of course check the result and based on that do the following action:

update_comment_meta($cid, 'mailchimp_subscribe', 'subscribed', 'on');

This will prevent the code from trying to subscribe someone multiple times and overloading the MailChimp API.

Conclusion: Subscribe those commenters!

People are interacting with your site already, they’re already giving you their email address, have them check that box, and make them subscribe to your list. I personally think it’s a great enhancement that would make a lot of people happy. If you’re not using Mailchimp but another email provider and are capable to do the same stuff with their API: please post about it, drop a comment here and I’ll add a link!

Disclosure: Mailchimp sponsors my newsletter and is a regular advertiser on my sites.


64 Responses to Turn your commenters into subscribers!

  1. w3expose
    By w3expose on 28 February, 2011

    Have anyone of you guys implemented this on a Genesis framework Child theme?

  2. Scott
    By Scott on 16 February, 2011

    Thanks for the great tip and article. I’m slowly figuring this out and was wondering if there is a way to do the comment.php portion with just a hook inside functions.php instead of modifying the actual theme file?

  3. Mark
    By Mark on 9 February, 2011

    Hi Yoast,

    Thanks for giving so much great usable content and tips etc.

    Will keep following your blog; thanks again!

  4. SGr33n
    By SGr33n on 9 February, 2011

    Great idea! I’m commenting, I will also subscribe to your newsletter :p
    Anyway maybe better to check it by default :p

  5. Cory Matheson
    By Cory Matheson on 8 February, 2011

    I am currently revamping my website and I installed Bhu_C2S to my WordPress blog. I have a mailchimp account as well and added all of the info needed in order to activate this plugin. However, I am not seeing any area to comment on the blog posts, nor am I seeing the ‘opt in’ button/option that I was hoping this plugin would provide.

    Wondering if there is something else that I need to do here in order to have the comment box and ‘opt in’ option showing on my posts. My website is currently a dev site until I can get this issue fixed. I may have to switch to another plugin if I can’t get this to work. Thanks

  6. Russell Thackston
    By Russell Thackston on 7 February, 2011

    Awesome idea!

    I’ve implemented it on both my websites. Only took a few minutes. It was actually easier to follow your code example than it was to install and configure the plug-in! Works like a charm.

    And, by the way… I subscribed to your newsletter.

  7. Hudson
    By Hudson on 7 February, 2011

    Would love an aweber plugin that does this.

  8. wilson usman
    By wilson usman on 6 February, 2011

    I’m confused because I have Thesis Theme. Do I just copy and past most of the code on the custom_functions.php?

  9. Arjen
    By Arjen on 6 February, 2011

    Nice idea! How popular is mailpress as newsletter??? I would love to see an integration with that service too.

  10. Azzam Sheikh
    By Azzam Sheikh on 6 February, 2011

    There is an existing plugin that already exist that works for AWeber, Mailchimp, Feedblitz, etc.

    I have wrote a follow up blog post to this and added the links to the plugin with a few of my thoughts:

    http://www.unleashthewealthwithin.net/how-one-line-of-text-increased-1000-subscribers-to-mailing-list/

    • Adam W. Warner
      By Adam W. Warner on 6 February, 2011

      Azzam, great write up and wow, this plugin seems like it “has it all”…going to test now. Thanks for letting us all know!

      • Adam W. Warner
        By Adam W. Warner on 6 February, 2011

        I have not been able to get this to work as expected so far. I am using MailChimp and have followed the plugin author’s directions, but so far no confirmation emails are getting sent.

        I’m going back to Bhu_C2S for now, it worked as expected right away, although I would like to see more features, it does the job:)

        • Azzam Sheikh
          By Azzam Sheikh on 6 February, 2011

          That is a real shame that it did not work.
          Did you check the forum over at wordpress for some already answered support?
          Obviously if the Bu_C2S version has worked then there is no need for you to take it any further.

          If bhuthecoder wants to combine it with his so it is working for all services including feed-burner that would be awesome and I could do another rewrite.

          I would like to see the option of a friendly pop-up if the text hits a blind spot and the commentator is prompted again

  11. Ronen Bekerman
    By Ronen Bekerman on 6 February, 2011

    Great stuff going on here!

    After reading this and hearing Yoast’s and Frank’s latest Podcast – How To Grow The Engagement On Your Blog

    Subscribers are not just email subscribers! I’m trying to increase my twitter followers just as much since I found that they respond very well to my tweets and do come visiting my blog & forums each time a send one out and engage with the new post / thread for a few minutes each time generating feedback and… you catch my drift :-)

    For a time the followers number increased in a steady rate, but lately seemed to hit a “glass ceiling” – not sure why is that yet (same for RSS actualy), the email subscribe rate keeps getting higher & higher.

    After reading Avinash Kaushik article (recommend on the aforementioned podcast page – so do check it out!) in his 9th recommendation he suggest to tweet the fact you comment about a topic on a blog.

    Any chance this latest plugin can be expanded to allow for following a twitter account and tweet the fact you just now commented about something with a link back to it?

    I’ve seen some implementations like – http://ttc.readscott.com/ but i don’t think the popover approach is very good… I much rather have it all done backstage in code and send it out with the comment submit button.

    Any thoughts about that?

  12. Bakkel
    By Bakkel on 6 February, 2011

    PLugin installed (great work btw), but the option isn’t showing up under my commentform. I use thesis framework, so I think I have to manually put the code in. My question: What is the code??

    • bhuthecoder
      By bhuthecoder on 6 February, 2011

      I just placed the code.You can copy the code from here : http://www.bhuthecoder.com/plugins/bhu_c2s/

      You can copy the code into your comments template

      • Bakkel
        By Bakkel on 6 February, 2011

        Code works like a charme! thx!!

        For anyone using Thesis:

  13. Sean Daily
    By Sean Daily on 6 February, 2011

    Very excited about this plugin and thanks for making it Bhu. Unfortunately, it’s not working on my 2 sites running the ThemeShift deVision template. =(

  14. bhuthecoder
    By bhuthecoder on 5 February, 2011

    HI all.
    I converted this code into a plugin.and tested with 3.04 version.soon i will add more services like feedburner,awber depending on people response.
    here the link : http://www.bhuthecoder.com/plugins/bhu_c2s/ or http://wordpress.org/extend/plugins/bhu-c2s/

    • Herman dailybits
      By Herman dailybits on 8 February, 2011

      +1 for feedburner support for this plugin…

    • Andre Stoelinga
      By Andre Stoelinga on 7 February, 2011

      It would be awesome if you’d be able to integrate more AR services. I’m using Imnica Mail since recently. Same features and reporting as aweber and they’ve overhauled the whole system. I hope you can find a way to include Imnica in your plugin… I get so many new users daily, it would be perfect to get some subscribers out of it…

      Greets,
      Andre

    • Ronen Bekerman
      By Ronen Bekerman on 6 February, 2011

      Cool! Thanks… If someone else is loading this – please add your feedback to it.

  15. Natalie Sisson
    By Natalie Sisson on 5 February, 2011

    What if you have Livefyre commenting system on your blog – I assume this won’t work with it?

    Thanks

  16. Hendrik
    By Hendrik on 4 February, 2011

    Awesome idea. that s for sure. I already use AutoChimp plugin to ask our users to subscribe to our newsletter.

    This way is pretty elegant, though.

    Is there a way that the user sees a checkbox asking him whether he wants to create an account on that website?

  17. Simin Lim
    By Simin Lim on 4 February, 2011

    I think it can be more easier if it is a plugin =D
    And change the Mailchimp to feedburner. Thanks :D

  18. rightways
    By rightways on 4 February, 2011

    Yoast, great idea!

    Can you provide me with a plugin or a short code?

    Thanks,
    Rightwaystan

  19. Bill (LoneWolf) Nickerson
    By Bill (LoneWolf) Nickerson on 4 February, 2011

    Anyone have a look at how this would work with the new AWeber API if possible? I plan on looking into this at some point, but if someone else has done the heavy lifting …

    Thanks for the work you’ve done on this Joost!

    • Lars Koudal
      By Lars Koudal on 4 February, 2011

      This is the code I have used in the past:

      http://www.aweber.com/scripts/addlead.pl?from=%email%&meta_web_form_id=YOUR_FORM_ID&unit=YOUR_LIST_ID&redirect=YOUR_REDIRECT_URL

      Replace ‘%email%’ with the e-mail to subscribe and the three other variables YOUR_FORM_ID, YOUR_LIST_ID and YOUR_REDIRECT_URL with your own.

      • Bill (LoneWolf) Nickerson
        By Bill (LoneWolf) Nickerson on 4 February, 2011

        The problem with this solution is that it would be called from your server rather than the user’s computer. That is a violation of AWeber’s terms. You can use this as the action of a form but not from a script on the server.

        I ran into this when I was looking at joining a giveaway event that was attempting to streamline the member signup process. They already had the name and email address when the user signed up for the event so they just passed it through the script. AWeber didn’t like it since it breaks the audit trail for signups.

        As I see it, there are a couple of options:
        1) Use JavaScript to hook the Post button (onSubmit) for the comment form and call the addlead.pl at that point if the checkbox is checked. This doesn’t get the benefit of spam filtering though. Do robots check boxes when they submit a spam comment?
        2) Have the comment code redirect to another page with a form to submit to AWeber. I’m not sure if you can “auto submit” that form or if the user would need to click on a button. That could then take them back to the original article.

        There are likely other options that will work as well. I wish I had more time to work on this right now, but it will have to wait a couple of days. Perhaps someone else will find a solution in the mean time.

  20. Sean
    By Sean on 3 February, 2011

    +1 for the plugin idea.. and fantastic post Joost!

  21. Fred Leo
    By Fred Leo on 3 February, 2011

    Does anyone know if this will work with Aweber?

    • Lars Koudal
      By Lars Koudal on 4 February, 2011

      Hi Fred

      Yes it will. Aweber offers API integration. To be honest its been over a year since I tested it, but I doubt they have removed functionality in the mean time :-)

  22. pete
    By pete on 3 February, 2011

    Good idea! Could it be a plugin for WordPress? Or maybe a post on WordPress Comments/trackbacks best practice …

    spam and suchlike is 95% of my ‘comments awaiting moderation :(

  23. Ivan Vucic
    By Ivan Vucic on 3 February, 2011

    While we are on the note about subscribing and that stuff, i noticed when i added feedburner to some of my blogs they dropped in se and got some penalties, do you have some experience with it?

  24. Lars Koudal
    By Lars Koudal on 3 February, 2011

    Haha Joost

    Perfect timing, lol! I have been working on re-releasing my newsletter plugin the past few days, and now I read this post by you! :-)

    Oh well, I guess I still have the market of WordPress uses who are not comfortable doing those changes manually.

    Great post as always, and I’m happy to see the life-style change seems to suit you :-)

    Best wishes from Barcelona.

  25. Zane Dickens
    By Zane Dickens on 3 February, 2011

    This is great idea! Thanks for the share. Its definitely being added to my list of urgent things to add to my blog.

    +1 for the plugin – even if its premium.

  26. Phillip Dews
    By Phillip Dews on 2 February, 2011

    Hi Joost,

    sounds like a great tip1 will be using this for sure! saying that though I am using Aweber and not mailchimp! will this work for Aweber as well? and how do I go about changing the code?

    thanks for this great tip!
    -Phillip Dews

  27. Tom Bathgate
    By Tom Bathgate on 2 February, 2011

    This is so useful, and funnily enough I got the email update about it whilst looking for a solution. However I am having a problem. Having followed the steps above, when I submit a comment and ticked to subscribe i get the following error:

    Missing argument 2 for ymc_subscription_add() at line 23 of my functions which is:
    function ymc_subscription_add( $cid, $comment ) {

    Any ideas? i’m using wordpress 3.0.4

  28. Theo
    By Theo on 1 February, 2011

    Smart, really smart.Will try this one for sure, bookmarked and thanks!

  29. Nick Stamoulis
    By Nick Stamoulis on 1 February, 2011

    Great idea for a strategically placed call to action. After reading a well written and informative blog post people are more likely to click on something that is easily assessable like this, rather than go looking for a way to subscribe. Thanks for sharing.

  30. Vlad Rascanu
    By Vlad Rascanu on 1 February, 2011

    Try giving something for free. You’ll see your number of subscribers go through the roof if it’s a good quality giveaway of course; generally it’s an ebook, a crash course on something, etc.

  31. Jesse McFarlane
    By Jesse McFarlane on 1 February, 2011

    Another great idea Yoast–targeting people who have already “bought in” just makes sense.

  32. Bakkel
    By Bakkel on 1 February, 2011

    Who, sweet! Only I’m not able to code this one so I’ll wait when Joss has finished the plugin!

  33. Joss
    By Joss on 1 February, 2011

    I’m going to make this into a plugin I think. Will post here when it’s done!

    • Ronen Bekerman
      By Ronen Bekerman on 6 February, 2011

      Yoast, this is a great idea!

      Not sure how the conversion rate on this position compares to a subscribe section at the end of each post… I almost sure the attention and a user willing to subscribe is highest just as he finishes to read a good post before he goes to comment on it a little bit lower on the page.

      One thing is for sure – all those that appreciated the post and for some unearthly reason did not subscribe… might do so before they press submit on a comment they decided to place so this is a great catch-all type of thing!

      add to that the redirection to a thank you page with yet more options to let them follow you and you got a complete envelope for getting the most subscribers possible.

      —-

      Joss,

      I tried to code this myself… but that little end part on Yoast article about the check results and not overloading the API got me a little Jiggy about it.

      I’ll sure appreciate a plugin implementation of this.

      All the best

      Ronen

    • Bill Brikiatis
      By Bill Brikiatis on 2 February, 2011

      Joss — It would be great if you could make the plugin work for Feedburner. I don’t know if you would consider it?

    • Adam W. Warner
      By Adam W. Warner on 1 February, 2011

      If you do this Joss, I’m sure many people will instantly become your biggest fan…including me:)

  34. Barbu
    By Barbu on 1 February, 2011

    Woww, the way your site handles the first visitor’s comments is very good, I like it. To redirect visitors to a page and to aske them to confirm the email looks good (as long as you can add extrs information about the site on te rediretion page). Can I ask you what is the mechanism behind all of these?
    Cheers

  35. Barbu
    By Barbu on 1 February, 2011

    Cannot this be done with an existing plugin?

    • Joost de Valk
      By Joost de Valk on 1 February, 2011

      Not that I know of and I have not currently got any plans to make a plugin out of it…

  36. Laura
    By Laura on 1 February, 2011

    I tried the above script but when I enter it all in my functions.php it gives me an error which makes make site unreachable. Or did I made the mistake that I should but it in my theme’s functions.php (instead of wordpress’ functions.php
    I would really really love to have a wordpress plugin for this!

    • Joost de Valk
      By Joost de Valk on 1 February, 2011

      Your theme indeed.

      • Laura
        By Laura on 1 February, 2011

        Thanks for the quick reply. One more thing: do I leave the space after/before the ‘ at API key and List ID?

  37. Jason Lombard
    By Jason Lombard on 1 February, 2011

    Killer tip! I will be using this one for sure. Thanks!!!

  38. quicoto
    By quicoto on 1 February, 2011

    Ok so I’m using the Google’s Feedburner E-mail newsletter. And I don’t wanna switch to Mailchimp.

    1) Do you know if that’s posible with the Feedburner API?

    2) An other question… I guess I could do it manually by sending me an e-mail with the new suscriber.

    Should I sent it where you wrote “// DO WORK” ?

    Thanks.

    Regards

    • quicoto
      By quicoto on 1 February, 2011

      Thanks. I’ve been digging a little, I don’t think it’s posible.

      I’m going for the second option.

      Regards

    • Joost de Valk
      By Joost de Valk on 1 February, 2011

      1) Hey, you’ll have to get someone to figure that out for you :)

      2) yes.

  39. SilverMagpies
    By SilverMagpies on 31 January, 2011

    Yoast –

    Can you write a plugin to do this for those of us who should not be messing with code! Please :-)


Check out our must read articles about Analytics