Created
August 24, 2020 21:48
-
-
Save cesgarma/8fe19c493b6261256fd810339fbca947 to your computer and use it in GitHub Desktop.
Integration with sendinblue.com from contact form 7.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
* SendInBlue integration with Contact Form 7 | |
* | |
*/ | |
// SendInBlue API KEY | |
if ( !defined( 'API_KEY_SENDINBLUE' ) ) { | |
define( 'API_KEY_SENDINBLUE', '<YOUR_SEND_IN_BLUE_API_KEY'); | |
} | |
// SendInBlue Hook URL 'createcontact' | |
if ( !defined( 'HOOK_URL_SENDINBLUE' ) ) { | |
define( 'HOOK_URL_SENDINBLUE', 'https://api.sendinblue.com/v3/contacts'); | |
} | |
// Create the new wordpress action hook before sending the email from CF7 | |
//add_action( 'wpcf7_before_send_mail', 'wpcf7_webhook_sendinblue' ); | |
function wpcf7_webhook_sendinblue( $cf7 ) { | |
$submission = WPCF7_Submission::get_instance(); | |
if ( $submission ) { | |
$posted_data = $submission->get_posted_data(); | |
// SendInBlue List ID - It is located next to the list name on SendInBlue List view. | |
$SendInBlue_ListID = 4; | |
if ( $cf7->id() == '177') { | |
// Simple Contact Form fields | |
$contact_attributes = array ( | |
'FULLNAME' => "{$posted_data["your-name"]}", | |
'TELEFONO' => "{$posted_data["your-phone"]}", | |
'COMPANY' => "{$posted_data["your-company"]}" | |
); | |
} elseif ( $cf7->id() == '204'){ | |
// Contact form requesting service and invoice amount | |
$contact_attributes = array ( | |
'FULLNAME' => "{$posted_data["your-name"]}", | |
'TELEFONO' => "{$posted_data["your-phone"]}", | |
'ESTADO' => "{$posted_data["your-estado"]}", | |
'NO_SERVICIO' => "{$posted_data["your-servicio"]}", | |
'ULTIMA_FACTURA' => "{$posted_data["your-factura"]}", | |
); | |
} | |
// Body data. | |
// https://developers.sendinblue.com/reference#createcontact | |
$data = json_encode(array( | |
'email' => "{$posted_data["your-email"]}", | |
'attributes' => $contact_attributes, | |
'listIds' => array($SendInBlue_ListID), | |
'updateEnabled' => true | |
)); | |
$args = array( | |
'method' => 'POST', | |
'body' => $data, | |
'headers' => array( | |
'accept' => 'application/json', | |
'Content-Type' => 'application/json', | |
'api-key' => API_KEY_SENDINBLUE | |
), | |
'timeout' => 30, | |
'redirection' => 10, | |
'httpversion' => '1.0', | |
); | |
$result = wp_remote_post (HOOK_URL_SENDINBLUE, $args); | |
if ( is_wp_error ( $result ) ) { | |
throw new Exception ( $result->get_error_message() ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment