someName
someName

Reputation: 41

Mailchimp for WP add subscriber programmatically

i am trying to add an user to a MailChimp list programmatically (so he is a subscriber of any emails i will send). I do have the pro-version of the MailChimp for WordPress plugin.

Is there a way to add - and remove a user (Email and three fields) to a list dynamically? There seems to be an API (http://developer.mc4wp.com/), but i did not found a function to do so.

Is there one?

Upvotes: 3

Views: 2346

Answers (2)

MHK Developer
MHK Developer

Reputation: 107

include('/MailChimp.php');
$MailChimp = new \DrewM\MailChimp\MailChimp("API-KEY");
$result = $MailChimp->get('lists');
$list_id = 'a0123a45f';  //  List Key
$result = $MailChimp->post("lists/$list_id/members", [
                'email_address' => [email protected],
                'status'        => 'subscribed',
                'merge_fields' => array('FNAME'=>'test', 'LNAME'=>'tester'),
            ]);

Upvotes: -1

leepowers
leepowers

Reputation: 38308

Use the mc4wp_get_api function to grab an instance of the MailChimp for WordPress API. Then call the subscribe() function add an email to a list:

$list_id = "2341ca4321";
$email = "[email protected]";
$api = mc4wp_get_api();
$api->subscribe($list_id, $email);
  • The subscribe() function returns a boolean. This return value simply reports if the subscribe request succeeded. Will return false if the user is already on the list
  • $list_id can be found when logged into MailChimp, looking under a list, Settings > List name and campaign defaults > List ID

Upvotes: 4

Related Questions