Last active
January 20, 2019 16:32
-
-
Save psaikali/95a4b8b039adef998e575db24181cfc8 to your computer and use it in GitHub Desktop.
WordPress : comment inscrire un nouvel utilisateur dans une liste MailChimp ?
This file contains 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 | |
namespace MSK\Blog\Mailchimp; | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
/** | |
* Inscrit un utilisateur dans une liste MailChimp | |
* | |
* @param string $email Adresse e-mail à inscrire | |
* @param [type] $user_id Identifiant utilisateur WP, pour sauvegarder l'ID MailChimp dans une usermeta | |
* @param boolean $double_optin S'il faut activer l'optin | |
* @return boolean | |
*/ | |
function add_email_to_mailchimp_list( $email = '[email protected]', $user_id = null, $double_optin = true ) { | |
$mc_api_key = 'votre_cle_api_mailchimp-us20'; | |
$mc_list_id = '368569b12f'; | |
$mc_datacenter = 'us20'; | |
$status = $double_optin ? 'pending' : 'subscribed'; | |
if ( empty( $mc_api_key ) || empty( $mc_list_id ) || empty( $mc_datacenter ) || ! is_email( $email ) ) { | |
return false; | |
} | |
$url = "https://{$mc_datacenter}.api.mailchimp.com/3.0/lists/{$mc_list_id}/members/"; | |
$body = [ | |
'email_address' => sanitize_email( $email ), | |
'status' => $status, | |
]; | |
$args = [ | |
'body' => wp_json_encode( $body ), | |
'headers' => [ | |
'Authorization' => "apikey {$mc_api_key}", | |
], | |
]; | |
$response = wp_remote_post( $url, $args ); | |
if ( ! is_wp_error( $response ) ) { | |
$response_body = json_decode( wp_remote_retrieve_body( $response ) ); | |
if ( isset( $response_body->id ) ) { | |
if ( ! is_null( $user_id ) ) { | |
update_user_meta( (int) $user_id, '_mc_user_id', sanitize_text_field( $response_body->id ) ); | |
} | |
return true; | |
} | |
} | |
return false; | |
} |
This file contains 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 | |
/** | |
* Inscrit un utilisateur dans MailChimp lorsqu'un utilisateur s'inscrit sur notre site WordPress | |
* | |
* @param int $user_id L'identifiant utilisateur WP créé | |
* @return void | |
*/ | |
function subscribe_to_mc_after_wp_user_regitration( $user_id ) { | |
$user = get_user_by( 'id', (int) $user_id ); | |
add_email_to_mailchimp_list( $user->data->user_email, (int) $user_id ); | |
} | |
add_action( 'user_register', __NAMESPACE__ . '\\subscribe_to_mc_after_wp_user_regitration' ); |
This file contains 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 | |
/** | |
* Inscrit un utilisateur dans MailChimp lorsqu'un acheteur vient de passer une commande WooCommerce | |
* | |
* @param int $order_id L'ID de la commande WooCommerce | |
* @return void | |
*/ | |
function subscribe_to_mc_after_woocommerce_order( $order_id ) { | |
$order = wc_get_order( $order_id ); | |
if ( ! $order || ! $order->get_billing_email() ) { | |
return; | |
} | |
add_email_to_mailchimp_list( $order->get_billing_email(), (int) $order->get_customer_id() ); | |
} | |
add_action( 'woocommerce_thankyou', __NAMESPACE__ . '\\subscribe_to_mc_after_woocommerce_order' ); |
This file contains 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 | |
/** | |
* Inscrit un utilisateur dans MailChimp lorsqu'un formulaire Contact Form 7 est envoyé | |
* | |
* @param WPCF7_ContactForm $contact_form Le formulaire de contact CF7 | |
* @return void | |
*/ | |
function subscribe_to_mc_after_cf7_submission( $contact_form ) { | |
$form_post = $contact_form->id(); | |
$submission = \WPCF7_Submission::get_instance(); | |
if ( $submission ) { | |
$email = $submission->get_posted_data( 'your-email' ); | |
if ( ! empty( $email ) > 0 ) { | |
add_email_to_mailchimp_list( $email ); | |
} | |
} | |
} | |
add_action( 'wpcf7_mail_sent', __NAMESPACE__ . '\\subscribe_to_mc_after_cf7_submission' ); |
This file contains 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 | |
/** | |
* Inscrit un utilisateur dans MailChimp lorsqu'un formulaire Gravity Forms est envoyé | |
* | |
* @param array $data Données du formulaire Gravity Forms | |
* @param array $form Le formulaire Gravity Forms | |
* @return void | |
*/ | |
function subscribe_to_mc_after_gravityforms_submission( $data, $form ) { | |
if ( isset( $data[1] ) && ! empty( $data[1] ) ) { | |
$email = sanitize_email( $data[1] ); | |
add_email_to_mailchimp_list( $email ); | |
} | |
} | |
add_action( 'gform_after_submission_1', __NAMESPACE__ . '\\subscribe_to_mc_after_gravityforms_submission', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment