Welcome Guest! Login? Checkout

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!



Polite Disclaimer: I am welcome, open and willing for corrections to be shared in the comments (with corrections being added to posts and credited), and the comments field should be used to promote discussion and make this post better. I do not know everything and if anybody finds a better way to do something, then by all means please share it below.

However, I'm unable to offer support to posts. The reason being is that WordPress has tens of thousands of plugins and millions of themes. As such, finding out exactly why code doesn't work with your setup is a long process. If you wish for me to look at your code, please use the priority support area.

Comments asking support will be left on the site, but there is no guarantee of answer.

    Comments are closed.