Mailchimp for WP add subscriber programmatically
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 returnfalse
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'), ]);