Mailchimp API 3.0 – PHP and AJAX subscription / unsubscription
https://rudrastyh.com/mailchimp-api/subscription.html
Mailchimp API 3.0 – PHP and AJAX subscription / unsubscription
Ok, if you are reading this post, you already know something about mailchimp and use default Mailchimp sign up forms maybe.
But what if you want your forms to be more customizable for your website purposes, such as:
original form design,
mailchimp subscription built-in another form (sign up checkbox in custom form — look at my comment form),
no confirmation emails,
subscribe or unsubscribe users via your website administration area etc.
First of all insert this function into your website code. If you use WordPress, it can be functions.php
file.
Ok, now let me show what each parameter means.
$email(string) I hope it is clear for you.$status(string) Subscriber status to set:
subscribed
unsubscribed
cleaned
pending
(confirmation email will be sent)
$list_id(string) Mailchimp list ID you want to add subscribers to. How to obtain it?$api_key(string) API key.$merge_fields(array) Subscriber fields, can be configured in a list settings. By default each mailchimp list has two merge fields (except email) — subscriber first name $merge_fields['FNAME']
and last name $merge_fields['LNAME']
.
How to get Mailchimp list ID?
Login to Mailchimp, go to Lists then click on the list title, then in list menu Settings > List name and defaults.
How to get Mailchimp API keys?
1. In main Mailchimp menu click on your picture and go to Account.
2. Then choose Extras > API keys.
3. That’s it. Click on Create A Key and your new API key will appear.
PHP Subscription example
Do not forget to copy the rudr_mailchimp_subscriber_status()
function to your website code (you can find it at the beginning of the post).
The very simple example. Do you think so?
AJAX Subscription example (WordPress-friendly)
Actually my blog is about WordPress, so the AJAX example will be adapted for WordPress.
Step 1. Sign Up form HTML
This form will work properly only if the JavaScript is enabled in user browser. You can also upgrade it to work both with JavaScript and without it.
Step 2. jQuery code to send asynchronous request
Make sure that jQuery library is included to your website pages.
Step 3. PHP code for your functions.php file. Errors Handling
Last updated