Sync email opt-outs to Mailchimp
When a subscriber opts out of Benecaster emails via the unsubscribe link, propagate the opt-out to your Mailchimp audience so the preference is consistent across systems. benecaster_email_resubscribed fires if they opt back in later. With Email Editor active, broadcast sends also respect the opt-out status automatically.
Code
<?php
add_action( 'benecaster_email_unsubscribed', function ( int $user_id, int $show_id, string $type ): void {
my_mailchimp_update_status( $user_id, 'unsubscribed' );
}, 10, 3 );
add_action( 'benecaster_email_resubscribed', function ( int $user_id, int $show_id, string $type ): void {
my_mailchimp_update_status( $user_id, 'subscribed' );
}, 10, 3 );
function my_mailchimp_update_status( int $user_id, string $status ): void {
$user = get_userdata( $user_id );
if ( ! $user instanceof WP_User ) {
return;
}
$hash = md5( strtolower( $user->user_email ) );
wp_remote_request( 'https://us1.api.mailchimp.com/3.0/lists/YOUR_LIST/members/' . $hash, [
'method' => 'PATCH',
'timeout' => 5,
'blocking' => false,
'headers' => [
'Authorization' => 'Basic ' . base64_encode( 'anystring:' . MY_MAILCHIMP_API_KEY ),
'Content-Type' => 'application/json',
],
'body' => wp_json_encode( [ 'status' => $status ] ),
] );
}