Are you looking to connect Gravity forms to MailPoet? Gravity Forms is the perfect plugin for WordPress website owners looking to create powerful forms quickly and easily. Mailpoet is a free email newsletter plugin for WordPress. It has its own SMTP email-sending solution, which solves website transactional email (and gravity form notification) sending problems.
However, if you’re looking to integrate Mailpoet as your email-sending solution, you may be surprised to find that there are limited free Gravity Forms integrations available. There is a plugin in the WordPress repository, but it hasn’t been updated in over two years and isn’t supported. Don’t worry, though – Mailpoet does have a simple API that is automatically distributed within the Mailpoet Plugin.
The Mailpoet API is implemented with a PHP class and becomes available once the Mailpoet plugin is loaded. As such, a code snippet hooked to the Gravity forms after the submission hook can be used to push relevant subscriber information from form entry data to a Mailpoet list.
As with other post-submission actions, I will use the gform_after_submission action hook. This gravity forms hook executes at the end of the submission process (after form validation, notification, and entry creation)
Step 1: Find your Mailpoet List ID
The most simple way to find it is to hover over the list and review the URL for the edit screen
https://yourdomain.com/wp-admin/admin.php?page=mailpoet-segments#/edit/7
In this case it is List number 7
Step 2: Add this Mailpoet List ID to your form as a hidden field
In the edit, screen of the Gravity form that you want to integrate with MailPoet, create a hidden field with the name Mailpoet_list_id, and set the default value to equal your list ID value.
Note the Form ID number as this is used in the gform_after_submission hook to facilitate applying this hook to a specific form (in this case, form ID 17).
Step 3: Create the code to push the form submission data to Mailpoet
The gform_after_submission action hook executes at the end of the submission process (after form validation, notification, and entry creation). I will use this hook to feed the form submission data to Mailpoet).
add_action( 'gform_after_submission_17', 'ns_post_to_mailpoet_17', 10, 2 );
function ns_post_to_mailpoet_17( $entry, $form ) {
}
Note we will be using the $entry object to grab the subscriber data we want to push to Mailpoet.
function ns_post_to_mailpoet_17( $entry, $form ) {
$gfbody = array(
'first_name' => rgar( $entry, '1.3' ),
'last_name' => rgar( $entry, '1.6' ),
'email' => rgar( $entry, '2' ),
'list_id' => rgar( $entry, '4' ),
'website' =>rgar( $entry, '3' ),
);
if (class_exists(\MailPoet\API\API::class)) {
// Get MailPoet API instance
$mailpoet_api = \MailPoet\API\API::MP('v1');
// Get available list so that a subscriber can choose in which to subscribe
$lists = $mailpoet_api->getLists();
$list_ids =array($gfbody["list_id"]);
$subscriber = [];
$subscriber['first_name'] = $gfbody["first_name"];
$subscriber['last_name'] = $gfbody["last_name"];
$subscriber['email'] = $gfbody["email"];
$subscriber['cf_1'] = $gfbody["website"];
$subscriber_form_fields = $mailpoet_api->getSubscriberFields();
// Check if subscriber exists. If subscriber doesn't exist an exception is thrown
try {
$get_subscriber = $mailpoet_api->getSubscriber($subscriber['email']);
} catch (\Exception $e) {}
try {
if (!$get_subscriber) {
// Subscriber doesn't exist let's create one
$mailpoet_api->addSubscriber($subscriber, $list_ids);
} else {
// In case subscriber exists just add them to new lists
$mailpoet_api->subscribeToLists($subscriber['email'], $list_ids);
}
} catch (\Exception $e) {
$error_message = $e->getMessage();
}
}
}
In the above example, I have Mailpoet custom field cf_1 as an additional field in Mailpoet to store the client website URL
Add this code to your theme functions.php file, and test the form submission.
Please note you will need to query the API for the any custom field ID numbers. I have done this using the following code set to output in a tools page I create using the add_menu_page() function
function ns_help_tools_admin(){
if (class_exists(\MailPoet\API\API::class)) {
// Get MailPoet API instance
$mailpoet_api = \MailPoet\API\API::MP('v1');
// Get available list so that a subscriber can choose in which to subscribe
$lists = $mailpoet_api->getLists();
$subscriber_form_fields = $mailpoet_api->getSubscriberFields();
echo '<h2>Lists</h2>';
echo '<pre>';
print_r($lists);
echo '</pre>';
echo '<h2>Subscriber Form Fields</h2>';
echo '<pre>';
print_r($subscriber_form_fields );
echo '</pre>';
}
}
Need help connecting Gravity Forms to Mailpoet?
Fill in the form to get started today!