2

I have used this after my form.But my entry does not go to the mailchimp account. Please tell me if anything missing. Please refer the below code.

<?php

// SUBSCRIBE TO MAILING LIST OPTION - ADD TO MAILCHIMP USING API
if ($_POST['emailUpdates'] == 'Yes')
{
    // Include Mailchimp API class
    require_once('MCAPI.class.php');

    // Your API Key: http://admin.mailchimp.com/account/api/
    $api = new MCAPI('xxxxxxxxx-us15');

    // Your List Unique ID: http://admin.mailchimp.com/lists/ (Click "settings")
    $list_id = "xxxxxxx";

    // Variables in your form that match up to variables on your subscriber
    // list. You might have only a single 'name' field, no fields at all, or more
    // fields that you want to sync up.
    $merge_vars = array(
        'FNAME' => $_POST['signup_first_name'],
        'LNAME' => $_POST['signup_last_name']
    );

    // SUBSCRIBE TO LIST
    if ($api->listSubscribe($list_id, $_POST['signup_user_email'], $merge_vars) === true)
    {
        $mailchimp_result = 'Success! Check your email to confirm sign up.';
    }
    else
    {
        $mailchimp_result = 'Error: ' . $api->errorMessage;
    }
}

?>
ekad
  • 14,436
  • 26
  • 44
  • 46
Priya2026
  • 87
  • 1
  • 2
  • 7

4 Answers4

0

try to add the list id with a single quote this is example what i used for my form

<?php
    require_once 'inc/MCAPI.class.php';
    $api = new MCAPI('xxxxxxxxxxx-us15');
    $merge_vars = array('FNAME'=>$_POST["fname"], 'LNAME'=>$_POST["lname"]);

    // Submit subscriber data to MailChimp
    // For parameters doc, refer to: http://apidocs.mailchimp.com/api/1.3/listsubscribe.func.php
    $retval = $api->listSubscribe( 'xxxxx', $_POST["email"], $merge_vars, 'html', false, true );

    if ($api->errorCode){
        echo "<h4>Please try again.</h4>";
    } else {
        echo "<h4>Thank you, you have been added to our mailing list.</h4>";
    }
?>
Priya2026
  • 87
  • 1
  • 2
  • 7
Aakanksh Patel
  • 603
  • 1
  • 8
  • 21
0

integrate mailchimp form without using sdk class

 $email= $_POST["email"];

$api_key = "dsfdfdsfdsdsfsfdgdfg-us15"; //api key
$list_id = "2f35dfgdgffec5c2"; // list id


  $url = 'https://us15.api.mailchimp.com/2.0/lists/subscribe.json?apikey='.$api_key.'&id='.$list_id.'&email[email]='.$email.'&double_optin=false&send_welcome=false';
echo callMailchimp($url);
function callMailchimp($url)
{
    $ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_SSL_VERIFYHOST => 2
    ));

    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

https://www.quora.com/How-do-I-use-PHP-cURL-to-access-the-new-MailChimp-API-v3-0

Suresh Suthar
  • 794
  • 8
  • 15
0

This is the code I used for one of my project I change some details like id and api_key as you can see I made custom shortcode for this and also you will see the location of user from where they fill up the form by IP

[mail_chimp_form]

<?php 
/*-------------------------------------------------------------------------------
    MAIL CHIMP API
-------------------------------------------------------------------------------*/

function rudr_mch_subscribe() {
    $list_id = 'XXXXX';
    $api_key = 'XXXXXXXX-us15';
    $ip = $_SERVER['REMOTE_ADDR'];
   $details = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));
    $cnt=$details->country; 
    $result = json_decode(rudr_mailchimp_subscriber_status($_POST['email'], 'subscribed', $list_id, $api_key, array('FNAME' => $firstname, 'LNAME' => $lastname),array('country_code'=>'".$cnt."')));
    // print_r($result);
            if ($resul->status == 400) {
                foreach ($result->errors as $error) {
                    echo '<p>Error: ' . $error->message . '</p>';
                }
            } elseif ($result->status == 'subscribed') {
                echo 'Thanks for Subscribing!';
            }
            die;
        }
add_action('wp_ajax_mailchimpsubscribe', 'rudr_mch_subscribe');
add_action('wp_ajax_nopriv_mailchimpsubscribe', 'rudr_mch_subscribe');
function rudr_mailchimp_subscriber_status( $email, $status, $list_id, $api_key, $merge_fields = array('FNAME' => '','LNAME' => ''),$location=array('country_code'=>'') ){
       function getUserIP()
    {
        $client  = @$_SERVER['HTTP_CLIENT_IP'];
        $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
        $remote  = $_SERVER['REMOTE_ADDR'];

        if(filter_var($client, FILTER_VALIDATE_IP))
        {
            $ip = $client;
        }
        elseif(filter_var($forward, FILTER_VALIDATE_IP))
        {
            $ip = $forward;
        }
        else
        {
            $ip = $remote;
        }

        return $ip;
    } 
    $ipAddr = getUserIP();
    $geoIP  = json_decode(file_get_contents("http://freegeoip.net/json/$ipAddr"), true);
    $lati=$geoIP['latitude'];
    $long=$geoIP['longitude'];
    $ip = $_SERVER['REMOTE_ADDR'];
    $details = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));
    $cnt=$details->country;
    $data = array(
        'apikey'        => $api_key,
        'email_address' => $email,
        'status'        => $status,
        'merge_fields' =>$merge_fields,
        'location' =>array(
               'latitude' =>$lati,
                'longitude' => $long,
                'country_code'=>$cnt
          ) 
    );
    $mch_api = curl_init(); // initialize cURL connection

    curl_setopt($mch_api, CURLOPT_URL, 'https://' . substr($api_key,strpos($api_key,'-')+1) . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . md5(strtolower($data['email_address'])));
    curl_setopt($mch_api, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Basic '.base64_encode( 'user:'.$api_key )));
    curl_setopt($mch_api, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
    curl_setopt($mch_api, CURLOPT_RETURNTRANSFER, true); // return the API response
    curl_setopt($mch_api, CURLOPT_CUSTOMREQUEST, 'PUT'); // method PUT
    curl_setopt($mch_api, CURLOPT_TIMEOUT, 10);
    curl_setopt($mch_api, CURLOPT_POST, true);
    curl_setopt($mch_api, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($mch_api, CURLOPT_POSTFIELDS, json_encode($data) ); // send data in json

    $result = curl_exec($mch_api);
    return $result;
}
function mail_chimp_code(){
 $current_user = wp_get_current_user();

?>
 <div class="chimpform">
   <div class="chimpform_inenr">
<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" id="mailchimp">
    <!-- for my website the site_url() function returns https://rudrastyh.com -->
    <input type="text" name="fname" placeholder="First name" style="display:none;" />
    <input type="text" name="lname" placeholder="Last name" style="display:none;" />
    <input type="email" name="email" id="mce-EMAIL" value="<?php echo $current_user->user_email ?>" placeholder="Your email address" required/>

    <input type="hidden" name="action" value="mailchimpsubscribe" />
    <!-- we need action parameter to receive ajax request in WordPress -->

    <div class="mail_left"><button>YES! I WANT TO IMPROVE MY CAREER</button></div>
    <div class="mail_right"><button class="pum-close popmake-close" type="button"/>NO THANKS, I DON'T WANT ANY HELP</button></div>
  </form>
   <p class="status"></p>
  </div>
</div>
<script>
jQuery(function($){
    $('#mailchimp').submit(function(){
        var mailchimpform = $(this);
        $.ajax({ 
            url:mailchimpform.attr('action'),
            type:'POST',
            data:mailchimpform.serialize(), 
            success:function(data){
                  $('#mailchimp').hide();
                 $('p.status').text(data);
                document.getElementById("mailchimp").reset();
            }
        });
        return false;
    });
});
</script>

<?php
}// End some_random_code()
add_shortcode( 'mail_chimp_form', 'mail_chimp_code' );
Aakanksh Patel
  • 603
  • 1
  • 8
  • 21
0

You can set up a sync from WordPress to Mailchimp without any code or plugins using zzBots.

You can read more about this sync here: https://www.zzbots.com/community/how-to/sync-new-wordpress-users-to-mailchimp

This specific sync, or integration, is designed to automatically sync new WordPress users to Mailchimp as new subscribers. Although, you can use zzBots to integrate these two app together in many different ways.

Disclaimer: I am affiliated with zzBots

  • You appear to be the author of the linked article. When linking to your own site or content (or content that you are affiliated with), you [must disclose your affiliation _in the answer_](/help/promotion) in order for it not to be considered spam. Having the same text in your username as the URL or mentioning it in your profile is not considered sufficient disclosure under Stack Exchange policy. – cigien Mar 13 '21 at 00:16