How to Add Subscribers to a MailPoet List via Zapier
Add contacts to a MailPoet subscriber's list via Zapier when the data is added to Google Sheets or any other application that Zapier supports.
You'll need:
- WP Zapier Integration plugin (version 1.4 or higher)
- An account on Zapier.com
You can use the update_user action which is a default function of the WordPress Zapier plugin. This will allow you to update WordPress users if you pass new data (if you don’t want to update the user you only need to pass through the user’s email as per the image below).
To get the MailPoet list ID you wish to add your subscribers to, go to your WordPress dashboard > MailPoet > Lists page. Here you will see the lists you’ve created within MailPoet. You may click the edit button and in the URL of the edit screen you will see the list’s ID:
When you map the data inside Zapier, you will add a variable with a comma-separated value for the multiple MailPoet list ID’s you want to add your users to. This can be dynamically added inside your trigger application (i.e. Google Sheets). The code snippet will create a MailPoet subscriber and add them to one or more specified lists.
Code Snippet
<?php /** * Add subscribers to multiple MailPoet lists when receiving data from Zapier with WP Zapier plugin. * i.e. Add users to MailPoet lists via Zapier when added to Google Sheets. * For full blog post visit - https://yoohooplugins.com/add-subscribers-mailpoet-zapier */ function add_subscribers_to_mailpoet_zapier( $user ) { //If lists aren't passed through just bail. if ( empty( $_REQUEST['lists'] ) ) { return $user; } if (class_exists(\MailPoet\API\API::class)) { $mailpoet_api = \MailPoet\API\API::MP('v1'); $sub = array( 'email' => sanitize_text_field( $_REQUEST['email'] ) ); // See if the user exists first. try { $subscriber = $mailpoet_api->getSubscriber( $sub['email'] ); } catch ( \Throwable $th ) {} // If the subscriber doesn't exist, add them. if ( ! $subscriber ) { try { $sub = $mailpoet_api->addSubscriber( $sub, $list_id ); } catch (\Throwable $th) {} } // Try add the user to lists. if ( $subscriber ) { $user_id = $subscriber['id']; $lists_id = explode( ',', $_REQUEST['lists'] ); $lists = array(); foreach( $lists_id as $key => $list_id ) { $lists[] = intval( $list_id ); } // add users to the lists. try { $subscribe = $mailpoet_api->subscribeToLists( $user_id, $lists ); } catch ( \Throwable $th ) {} } } return $user; } add_filter( 'wp_zapier_before_update_user', 'add_subscribers_to_mailpoet_zapier', 10, 1 );