/**
* Enable Integration between Contact Form 7 and MailChimp.
*
* @param WPCF7_Submission $cf7 Contact form submitted data.
*
* @return boolean
*/
function dorzki_send_leads_to_mailchimp( $cf7 ) {
// Integration settings.
$form_id = 0;
$list_id = 'XXXXXXX';
$api_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-us2';
if ( $form_id !== $cf7->id() ) {
return false;
}
$submit = \WPCF7_Submission::get_instance();
$data = $submit->get_posted_data();
if ( ! empty( $data ) ) {
$key_parts = explode( '-', $api_key );
$user_hash = md5( strtolower( $data['newsletter_mail'] ) );
$url = sprintf( 'https://%s.api.mailchimp.com/3.0/lists/%s/members/%s', $key_parts[1], $list_id, $user_hash );
$response = wp_remote_request( $url, [
'method' => 'put',
'headers' => [
'Authorization' => "Basic {$api_key}",
],
'body' => json_encode( [
'email_address' => $data['newsletter_mail'],
'status' => 'subscribed',
'merge_fields' => [],
] ),
] );
return ( ! is_wp_error( $response ) );
}
return false;
}
add_action( 'wpcf7_before_send_mail', 'dorzki_send_leads_to_mailchimp' );
Credit: dorzki.co.il
How to use it?
Usually adding functionality to a WordPress site is by creating a plugin.
However you can simply copy the code and paste it in your child theme’s functions.php
file at the end of the file, just before ?>
.
For JavaScript code you will need to add them to your theme’s main JavaScript file.
Having issues with the code?
If the code doesn’t work or you get an error please let us know in the comments section.
We will do our best to fix it as-soon-as-possible and update the code on this page.
Comments