Skip to content

Instantly share code, notes, and snippets.

@AntonioLeutsch
Last active July 1, 2020 18:26
Show Gist options
  • Save AntonioLeutsch/e9350df09c6e478929177d6604d8ccb6 to your computer and use it in GitHub Desktop.
Save AntonioLeutsch/e9350df09c6e478929177d6604d8ccb6 to your computer and use it in GitHub Desktop.
Contact Form 7 to Sendy.co Newsletter Instance
<?php
// Feel free to save your $list_id and $api_key where you want.
/**
* Send Contact Form 7 to Newsletter
*
* @param $form object WPCF7_ContactForm
* @param $abort boolean
* @param $submission object WPCF7_Submission
*/
function subscribe_to_newsletter( $form, &$abort, $submission ) {
/**
* @var string $instance
* Your Sendy Instance URL
*/
$instance = 'https://your-sendy-instance.com';
/**
* @var string $api_key
* Your Sendy API key
*/
$api_key = 'xxx';
/**
* @var array $newsletter_form_ids
* contains the form id's from contact form 7
*/
$newsletter_form_ids = [
5, // Form Footer
6, // Form Sidebar
];
if ( in_array( $form->id, $newsletter_form_ids ) ) {
/**
* @var string $list_id
* The id of your Sendy list
*
*/
$list_id = null;
/** Check which Form where using and apply the right List ID */
switch ($form->id) {
case '5' :
$list_id = 'xxx';
break;
case '6' :
$list_id = 'zzz';
break;
}
/**
* @var array $posted_data
* the fields that have been submitted
*/
$posted_data = $submission->get_posted_data();
/**
* @var \http\QueryString $postdata
* build the body we want to send to sendy
*/
$postdata = http_build_query(
[
'email' => $posted_data['your-email'],
'name' => $posted_data['your-name'],
'list' => $list_id,
'api_key' => $api_key,
'boolean' => "true",
]
);
/**
* @var array $opts
* Arguments for wp_remote_post
*/
$opts = [
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'body' => $postdata,
];
/**
* Post the Request to Sendy
*/
$request = wp_remote_post( $instance . '/subscribe', $opts );
/**
* Get the Response
*/
$response = wp_remote_retrieve_response_message( $request );
/**
* Check if the Responds returns a success message
* if not throw a error and abort the submission
*/
if ( $response !== 'OK' ) {
$abort = true;
$submission->set_response( $response );
}
}
}
add_action( 'wpcf7_before_send_mail', 'subscribe_to_newsletter', 10, 3 );
* @var \http\QueryString $postdata
* build the body we want to send to sendy
*/
$postdata = http_build_query(
[
'email' => $posted_data['your-email'],
'name' => $posted_data['your-name'],
'list' => $list_id,
'api_key' => $api_key,
'boolean' => "true",
]
);
/**
* @var array $opts
* Arguments for wp_remote_post
*/
$opts = [
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'body' => $postdata,
];
/**
* Post the Request to Sendy
*/
$request = wp_remote_post( $instance . '/subscribe', $opts );
/**
* Get the Response
*/
$response = wp_remote_retrieve_response_message( $request );
/**
* Check if the Responds returns a success message
* if not throw a error and abort the submission
*/
if ( $response !== 'OK' ) {
$abort = true;
$submission->set_response( $response );
}
}
}
add_action( 'wpcf7_before_send_mail', 'subscribe_to_newsletter', 10, 3 );
<?php
// Feel free to save your $list_id and $api_key where you want.
/**
* Send Contact Form 7 to Newsletter
*
* @param $form object WPCF7_ContactForm
* @param $abort boolean
* @param $submission object WPCF7_Submission
*/
function subscribe_to_newsletter( $form, &$abort, $submission ) {
/**
* @var array $newsletter_form_ids
* contains the form id's from contact form 7
*/
$newsletter_form_ids = [ 5 ];
if ( in_array( $form->id, $newsletter_form_ids ) ) {
/**
* @var string $instance
* Your Sendy Instance URL
*/
$instance = 'https://your-sendy-instance.com';
/**
* @var string $list_id
* The id of your Sendy list
*
*/
$list_id = 'xxx';
/**
* @var string $api_key
* Your Sendy API key
*/
$api_key = 'xxx';
/**
* @var array $posted_data
* the fields that have been submitted
*/
$posted_data = $submission->get_posted_data();
/**
* @var \http\QueryString $postdata
* build the body we want to send to sendy
*/
$postdata = http_build_query(
[
'email' => $posted_data['your-email'],
'name' => $posted_data['your-name'],
'list' => $list_id,
'api_key' => $api_key,
'boolean' => "true",
]
);
/**
* @var array $opts
* Arguments for wp_remote_post
*/
$opts = [
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'body' => $postdata,
];
/**
* Post the Request to Sendy
*/
$request = wp_remote_post( $instance . '/subscribe', $opts );
/**
* Get the Response
*/
$response = wp_remote_retrieve_response_message( $request );
/**
* Check if the Responds returns a success message
* if not throw a error and abort the submission
*/
if ( $response !== 'OK' ) {
$abort = true;
$submission->set_response( $response );
}
}
}
add_action( 'wpcf7_before_send_mail', 'subscribe_to_newsletter', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment