Skip to content

Instantly share code, notes, and snippets.

@goranefbl
Created May 29, 2025 17:04
Show Gist options
  • Save goranefbl/e52b0685260e7b0aa80da91400338e34 to your computer and use it in GitHub Desktop.
Save goranefbl/e52b0685260e7b0aa80da91400338e34 to your computer and use it in GitHub Desktop.
WPGens Referral Mailchimp Connection
<?php
add_action('wpgens_raf_new_user_referral_id', 'send_referral_to_mailchimp', 10, 2);
function send_referral_to_mailchimp($referral_id, $user_id) {
$user = get_userdata($user_id);
if (!$user) {
return;
}
$email = $user->user_email;
$first_name = $user->first_name;
$last_name = $user->last_name;
$referral_code = $referral_id; // Assuming this is the referral code
// Send to Mailchimp
send_to_mailchimp($email, $first_name, $last_name, $referral_code);
}
function send_to_mailchimp($email, $first_name, $last_name, $referral_code) {
$api_key = 'YOUR_API_KEY';
$list_id = 'YOUR_AUDIENCE_ID';
$data_center = substr($api_key, strpos($api_key, '-') + 1); // Extract data center from API key
$member_id = md5(strtolower($email));
$url = "https://$data_center.api.mailchimp.com/3.0/lists/$list_id/members/$member_id";
$json = json_encode([
'email_address' => $email,
'status_if_new' => 'subscribed',
'merge_fields' => [
'FNAME' => $first_name,
'LNAME' => $last_name,
'REFCODE' => $referral_code,
]
]);
$response = wp_remote_request($url, [
'method' => 'PUT',
'headers' => [
'Authorization' => 'apikey ' . $api_key,
'Content-Type' => 'application/json',
],
'body' => $json,
]);
if (is_wp_error($response)) {
error_log('Mailchimp error: ' . $response->get_error_message());
} else {
error_log('Mailchimp response: ' . wp_remote_retrieve_body($response));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment