Automatically sending newsletter subscribers from InstaWP to Mailchimp
This GraphQL query captures the email from the visitors who ticked the "Subscribe to mailing list" checkbox from InstaWP (when creating a new sandbox site), and subscribes this email to a Mailchimp list:
query HasSubscribedToNewsletter {
hasSubscriberOptin: _httpRequestHasParam(name: "marketing_optin")
subscriberOptin: _httpRequestStringParam(name: "marketing_optin")
isNotSubscriberOptinNAValue: _notEquals(value1: $__subscriberOptin, value2: "NA")
subscribedToNewsletter: _and(values: [$__hasSubscriberOptin, $__isNotSubscriberOptinNAValue])
@export(as: "subscribedToNewsletter")
}
query MaybeCreateContactOnMailchimp
@depends(on: "HasSubscribedToNewsletter")
@include(if: $subscribedToNewsletter)
{
subscriberEmail: _httpRequestStringParam(name: "email")
mailchimpUsername: _env(name: "MAILCHIMP_API_CREDENTIALS_USERNAME")
@remove
mailchimpPassword: _env(name: "MAILCHIMP_API_CREDENTIALS_PASSWORD")
@remove
mailchimpListMembersJSONObject: _sendJSONObjectItemHTTPRequest(input: {
url: "https://us7.api.mailchimp.com/3.0/lists/{listCode}/members",
method: POST,
options: {
auth: {
username: $__mailchimpUsername,
password: $__mailchimpPassword
},
json: {
email_address: $__subscriberEmail,
status: "subscribed"
}
}
})
}
And define in wp-config.php
:
define( 'MAILCHIMP_API_CREDENTIALS_USERNAME', '{ username }' );
define( 'MAILCHIMP_API_CREDENTIALS_PASSWORD', '{ password }' );
Step by step analysis of the GraphQL query permalink
Read blog posts:
- 🚀 Automatically sending the newsletter subscribers from InstaWP to Mailchimp
- 👨🏻🏫 GraphQL query to automatically send the newsletter subscribers from InstaWP to Mailchimp
Extensions referenced in this recipe permalink
- Field Response Removal
- Field To Input
- HTTP Client
- HTTP Request via Schema
- Multiple Query Execution
- PHP Constants and Environment Variables via Schema
- PHP Functions via Schema
- Response Error Trigger
(They are all included in “Application Glue & Automator” Bundle, “All Extensions” Bundle)