Adding a “Click To Subscribe” Box to Comment Forms Using Gravity Forms

Recently I’ve been having an issue with one of my sites that I wanted to start building an email list. One problem was that – due to the nature of the site – I couldn’t use my mailing list of choice of Mailchimp hooked in with WP Email Capture. I decided to use Gravity Forms to capture email addresses, and also I wanted a “Subscribe to Newsletter as you Comment” button (the site receives a lot of comments). So I wanted people – when they tick the box – to be added as an entry in Gravity Forms, which can then be exported.

Thankfully, this is quite straightforward to do using a couple of functions, one of which is native to WordPress, the other is using the Gravity Forms API.

Add A Comment Field to WordPress

WordPress allows you to add a comment field to the comment form using the comment_form_default_fields action. This action is run after the Name/Email/Website fields but before the textarea box, allowing some control of what you wish to add. We’re going to add a checkbox entitled “addtoemail” which can be ticked.

Saving to Gravity Form Using comment_post

The next step is to save comment data (such as their name and email address) to a gravity form when they leave a comment. We want to do this using the comment_post action. This action is run just after the comment is saved into the database, so we can check the commenter’s name and email, and add it to Gravity Forms.

One other thing we can get is the comment status. This should tell us what WordPress thinks of this comment. We need to run a spam check as if the comment is spam, we want to avoid clogging up our database with spammy email addresses. So if we pass the $comment_status variable, we will know if the comment is spam, and if it is, we should ignore this.

Here is the function that adds this functionality.

Try this yourself if you like, see if this works for you. Any comments and questions, please let us know!

Similar Posts