Register a newsletter subscriber from InstaWP to Mailchimp

🔥 This query is already included in Gato GraphQL, as Persisted Query “Register a newsletter subscriber from InstaWP to Mailchimp”.

InstaWP allows visitors to launch a sandbox site to test a plugin, while allowing them to subscribe to the newsletter:

Launching a testing site in InstaWP to test Gato GraphQL
Launching a testing site in InstaWP to test Gato GraphQL

We can capture these emails by providing a webhook to InstaWP, under the template's "Advanced Options" tab:

Advanced options for InstaWP templates
Advanced options for InstaWP templates

The webhook will be a persisted query with the query below. This query captures the email from the visitors, 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 RegisterNewsletterSubscribeFromInstaWPToMailchimpList(
# mailchimpDataCenterCode: Code for the data center of your account on Mailchimp (See: https://mailchimp.com/developer/marketing/docs/fundamentals/#api-structure)
$mailchimpDataCenterCode: String!
# Audience ID for the list on Mailchimp to which to subscribe the email
$mailchimpAudienceID: String!
)
@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

mailchimpAPIEndpoint: _sprintf(
string: "https://%s.api.mailchimp.com/3.0/lists/%s/members",
values: [$mailchimpDataCenterCode, $mailchimpAudienceID]
)

mailchimpListMembersJSONObject: _sendJSONObjectItemHTTPRequest(input: {
url: $__mailchimpAPIEndpoint,
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 }' );

Using this webhook, when creating a new testing site on InstaWP, and the user signs up to the newsletter, the visitor's email is automatically added to the Mailchimp list:

Email automatically added to Mailchimp list
Email automatically added to Mailchimp list

Extensions required for this query permalink

  1. Field Response Removal permalink

    Remove the output of a field from the response.

  2. Field To Input permalink

    Retrieve the value of a field, manipulate it, and input it into another field or directive, all within the same operation.

  3. HTTP Client permalink

    Addition of fields to execute HTTP requests against a webserver and fetch their response.

  4. HTTP Request via Schema permalink

    Addition of fields to retrieve the current HTTP request data.

  5. Multiple Query Execution permalink

    Combine multiple queries into a single query, sharing state across them and executing them in the requested order.

  6. PHP Constants and Environment Variables via Schema permalink

    Query the value from an environment variable or PHP constant.

  7. PHP Functions via Schema permalink

    Manipulate the field output using standard programming language functions available in PHP.

Bundles containing all extensions required for this query permalink

  1. “All in One Toolbox for WordPress” Bundle permalink

    Achieve all superpowers: All of Gato GraphQL extensions, in a single plugin

  2. “Better WordPress Webhooks” Bundle permalink

    Easily create webhooks to process incoming data from any source or service using advanced tools, directly within the wp-admin

  3. “Private GraphQL Server for WordPress” Bundle permalink

    Use GraphQL to power your application (blocks, themes and plugins), internally fetching data without exposing a public endpoint

  4. “Tailored WordPress Automator” Bundle permalink

    Create workflows to automate tasks (to transform data, automatically caption images, send notifications, and more)

  5. “Versatile WordPress Request API” Bundle permalink

    Interact with any external API and cloud service, posting and fetching data to/from them

Tutorial lessons explaining how the query works permalink