简体   繁体   中英

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?

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 = "subscriber@email.com";
$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
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' => test@test.com,
                'status'        => 'subscribed',
                'merge_fields' => array('FNAME'=>'test', 'LNAME'=>'tester'),
            ]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM