Skip to content

Instantly share code, notes, and snippets.

@1naveengiri
Created September 7, 2018 07:59
Show Gist options
  • Save 1naveengiri/bbc61149253165d4c50aa3e64cf1a0b9 to your computer and use it in GitHub Desktop.
Save 1naveengiri/bbc61149253165d4c50aa3e64cf1a0b9 to your computer and use it in GitHub Desktop.
Contact form 7 and Mailchimp Integration without plugin with action URL
<?php
/**
* This is a demo of mailchimp integration with contact form 7 with the help of Action URL.
*
* You can get these action URL from the mailchimp list
* follow this https://www.youtube.com/watch?v=sybmI8HgxFo
*
*/
// define the wpcf7_mail_sent callback
function cf7_action_wpcf7_mail_sent( $contact_form ) {
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
$posted_data = $submission->get_posted_data();
}
$contact_form_id = $contact_form->id();
if ( 123 == $contact_form_id ) { // check for contact form id
if ( $submission ) {
$posted_data = $submission->get_posted_data();
}
$target_data = array(
'EMAIL' => $posted_data['your-email'],
'FNAME' => $posted_data['your-name'],
'LNAME' => $posted_data['your-last'],
);
// here I am getting selected mailchimp mailing list. Added in contact form 7 as a checkbox.
$maillist = ( isset( $posted_data['checkbox-442'] ) && !empty( $posted_data['checkbox-442'] ))? $posted_data['checkbox-442']: array();
if( in_array('Technical', $maillist )){
$url = 'https://escapehunt.us19.list-manage.com/subscribe/post?u=87a3f3fa58cd92b3eee996143&amp;id=4fa993ecd1';
$response = wp_remote_post( $url, array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'body' => $target_data,
)
);
}
if( in_array('EscapeHunt', $maillist )){
$url = 'https://escapehunt.us19.list-manage.com/subscribe/post?u=87a3f3fa58cd92b3eee996143&amp;id=214fa81b19';
$response = wp_remote_post( $url, array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'body' => $target_data,
)
);
}
}
};
// add the action
add_action( 'wpcf7_mail_sent', 'cf7_action_wpcf7_mail_sent', 10, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment