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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /** * Add an "Add to email" checkbox to a comment form * * @link http://www.winwar.co.uk/2015/03/adding-a-click-to-subscribe-box-to-comment-forms-using-gravity-forms/?utm_source=codesnippet * * @param array $fields All fields within the comment form * @return array */ function winwar_add_to_email_list_field( $fields ) { $fields['add-to-email'] = '<p class="comment-form-public"> <input id="addtoemail" name="addtoemail" type="checkbox" /> <label for="addtoemail"> Check this box to add yourself to our email list. </label></p>'; return $fields; } add_filter( 'comment_form_default_fields', 'winwar_add_to_email_list_field' ); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | /** * Save the comment to gravity forms * * @link http://www.winwar.co.uk/2015/03/adding-a-click-to-subscribe-box-to-comment-forms-using-gravity-forms/?utm_source=codesnippet * * @param integer $comment_ID The comment ID * @param string $comment_status The status of the comment * @return void */ function winwar_save_comment( $comment_ID, $comment_status ) { if ( 'spam' !== $comment_status ) { $save_meta_checkbox = $_POST['addtoemail']; if ( $save_meta_checkbox == 'on' ) { $entry = array( 'form_id' => 1, // Change form ID to your Gravity Form ID 1 => esc_attr( $_POST['name'] ), // Change 1 to the field ID in your Gravity Form for Name 2 => esc_attr( $_POST['email'] // Change 2 to the field ID in your Gravity Form for Email ) ); $entry_id = GFAPI::add_entry( $entry ); if ( is_wp_error( $entry_id ) ) { wp_die( $entry_id->get_error_message() ); } } } } add_action( 'comment_post', 'winwar_save_comment', 10, 2 ); |
Try this yourself if you like, see if this works for you. Any comments and questions, please let us know!





