A common requirement on marketing sites is to automatically subscribe a user to a Mailchimp list when they submit a contact or registration form. Mailchimp provides a straightforward API endpoint for this, and WordPress makes it easy to call it with wp_remote_post().
Problem: After a visitor submits a form on your WordPress site, you want to add them to a Mailchimp audience automatically — without installing a full Mailchimp integration plugin.
Solution: Use the Mailchimp Marketing API v3 — send a PUT request to the /lists/{list_id}/members/{subscriber_hash} endpoint from a wp_remote_request() call in your form submission handler, with the MD5 hash of the lowercase email as the subscriber hash.
The function below sends a subscriber request to the Mailchimp API v3 after a form is processed. Replace YOUR_API_KEY and YOUR_LIST_ID with your Mailchimp credentials:
function subscribe_to_mailchimp( $email, $first_name = '', $last_name = '' ) {
$api_key = 'YOUR_API_KEY';
$list_id = 'YOUR_LIST_ID';
$dc = substr( $api_key, strpos( $api_key, '-' ) + 1 ); // e.g. 'us6'
$response = wp_remote_post(
"https://{$dc}.api.mailchimp.com/3.0/lists/{$list_id}/members",
[
'headers' => [
'Authorization' => 'Basic ' . base64_encode( 'anystring:' . $api_key ),
'Content-Type' => 'application/json',
],
'body' => json_encode( [
'email_address' => sanitize_email( $email ),
'status' => 'subscribed', // or 'pending' for double opt-in
'merge_fields' => [
'FNAME' => sanitize_text_field( $first_name ),
'LNAME' => sanitize_text_field( $last_name ),
],
] ),
]
);
if ( is_wp_error( $response ) ) {
return false;
}
$code = wp_remote_retrieve_response_code( $response );
return in_array( $code, [ 200, 204 ], true );
}
Hook it into Contact Form 7's submission action:
add_action( 'wpcf7_mail_sent', 'cf7_subscribe_to_mailchimp' );
function cf7_subscribe_to_mailchimp( $contact_form ) {
$submission = WPCF7_Submission::get_instance();
if ( ! $submission ) return;
$data = $submission->get_posted_data();
subscribe_to_mailchimp(
$data['your-email'] ?? '',
$data['your-first-name'] ?? '',
$data['your-last-name'] ?? ''
);
}
NOTE: Store your Mailchimp API key in wp-config.php as a constant — never hardcode it in a theme or plugin file that might end up in version control. Use 'status' => 'pending' instead of 'subscribed' if your audience requires double opt-in to comply with GDPR and CAN-SPAM regulations.