Last active
August 29, 2015 14:14
-
-
Save JudeRosario/24929afbaae4a0b84213 to your computer and use it in GitHub Desktop.
Integrate Membership with MailChimp API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action('register_user','mailchimp_add_to_list') | |
function mailchimp_add_to_list ($user_id) | |
{ | |
// Get Full Name and Email from BuddyPress | |
$name = bp_core_get_user_displayname( $user_id ); | |
$email = bp_core_get_user_email($user_id); | |
$fullname = explode(" ",$name); | |
$fname = $fullname[0]; | |
$lname = $fullname[1]; | |
// Settings for this subscriber | |
$double_optin=true; | |
$update_existing=false; | |
$replace_interests=true; | |
$send_welcome=false; | |
$email_type = 'html'; | |
// Name of the Subscriber added here | |
$merges = array('FNAME'=>$fname, 'LNAME'=>$lname); | |
// Data Array that gets sent to MailChimp | |
// IMPORTANT : Put API Key + List ID in this array | |
$data = array( | |
'email_address'=>$email, | |
'apikey'=>$apikey, // Put API Key here | |
'merge_vars' => $merges, | |
'id' => $listId, // Put List ID Here or remove this line if you don't have one | |
'double_optin' => $double_optin, | |
'update_existing' => $update_existing, | |
'replace_interests' => $replace_interests, | |
'send_welcome' => $send_welcome, | |
'email_type' => $email_type | |
); | |
// JSON over XML-RPC because its lightweight adnd fast | |
$payload = json_encode($data); | |
// The url to send this to, Im taking that your data center is us7 based on your thread | |
$submit_url = "http://us7.api.mailchimp.com/1.3/?method=listSubscribe"; | |
// cURL to send the data over | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $submit_url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($payload)); | |
// Get the response and process it | |
$result = curl_exec($ch); | |
curl_close ($ch); | |
$response = json_decode($result); | |
// This bit is optional | |
if ($response->error){ | |
// Put a success message or callback code here (Optional) | |
} else { | |
// If the add user operation fails, put the callback code here (Optional) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Copy Paste in
functions.php
or Site specific plugin and new members automatically get added to specified List