
How to Get MailChimp Auto Subscription upon User Registration
- April 15, 2016
- Leave a comment
The most popular email marketing platform MailChimp provides many useful features to perform marketing campaigns and send newsletters by creating subscribers’ list. In this article, we will describe the method of auto subscription upon user registration for a WordPress website.
Action hooks are provided by WordPress to attach a custom function with wp functions and then WordPress perform tasks on different actions. For user registration action, user_register hook provided by WordPress has been used. You need to attach the pt_mailchimp_subscribe function with user_register hook. This will send a subscription request to MailChimp upon successful registration of the user.
pt_mailchimp_subscribe function requires a User ID sent by action hook on its call inside wp_insert_user function. You need to get MailChimp API Key and Subscribers List ID to enter in pt_mailchimp_subscribe function’s code.
Now, add the following code in Theme‘s functions.php file or you can use it for any custom plugin:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
<?php function pt_mailchimp_subscribe( $user_id ){ // Stop action is user_id not found if( ! is_numeric( $user_id ) ) return; // Get wordpress user by ID $user = get_user_by( 'ID', $user_id ); // Setting up mailchimp API key $apikey = 'Enter Mailchimp API Here'; // Setting up mailchimp subscription list id $listid = 'Enter Subscribers List ID Here'; // Setting up server address to send request for subscription $server = substr( $apikey, strrpos( $apikey, '-' )+1 ); // Setting up user's data to send in request parameters $email = $user->user_email; $fname = $user->first_name; $lname = $user->last_name; $auth = base64_encode( 'user:'.$apikey ); $data = array( 'apikey' => $apikey, 'email_address' => $email, 'status' => 'subscribed', 'merge_fields' => array( 'FNAME' => $fname, 'LNAME' => $lname ) ); // Converting array to json format $json_data = json_encode($data); // Initializing cURL $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://'.$server.'.api.mailchimp.com/3.0/lists/'.$listid.'/members/'); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Authorization: Basic '.$auth)); curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data); $result = json_decode(curl_exec($ch)); curl_close($ch); if( $result->status == 'subscribed' ){ // Subscription done } } // Attaching function with wordpress action hook. add_action( 'user_register', 'pt_mailchimp_subscribe' ); ?> |
Hello! Thank you for that!
The e-mail is subscribing very well, but the first name and last name is not going to the mailchimp list. Could you help me with that? Thank you!
Regards,
Guilherme