Action
Dynamic
uncanny-automator
automator_mailchimp_webhook_received_{dynamic}
> **Note:** This is a dynamic hook. The actual hook name is constructed at runtime. Fires when Mailchimp webhooks are received, allowing for custom processing of the webhook body data.
add_action( 'automator_mailchimp_webhook_received_{dynamic}', $callback, 10, 1 );
Description
Fires after Automator successfully receives and parses webhook data from Mailchimp. Developers can use this action to execute custom logic based on the specific Mailchimp event type received. The dynamic portion of the hook name, `{dynamic}`, will be replaced by the actual event type. The `$body` parameter contains the full webhook payload.
Usage
add_action( 'automator_mailchimp_webhook_received_{dynamic}', 'your_function_name', 10, 1 );
Parameters
-
$body(mixed) - This parameter contains the raw body of the incoming Mailchimp webhook request.
Examples
<?php
/**
* Example of how to hook into the 'automator_mailchimp_webhook_received_{dynamic}' action.
* This example processes a 'subscribe' event from Mailchimp.
*
* @param mixed $body The raw data received from the Mailchimp webhook.
*/
function my_mailchimp_subscribe_handler( $body ) {
// Ensure we are dealing with a subscription event. The dynamic part of the hook
// would have already filtered for the correct event type before reaching this function.
// However, it's good practice to still check if the data structure is as expected.
// Let's assume the $body is an array and contains information about the subscriber.
// We'll log the email address of the subscriber.
if ( is_array( $body ) && isset( $body['data']['email_address'] ) ) {
$subscriber_email = $body['data']['email_address'];
$list_id = isset( $body['data']['list_id'] ) ? $body['data']['list_id'] : 'N/A';
// In a real-world scenario, you would:
// 1. Get the WordPress user associated with this email, if any.
// 2. Update user meta with Mailchimp subscription status.
// 3. Trigger other automations or notifications.
// 4. Add the subscriber to a specific internal WordPress list or group.
// For this example, we'll just log the information.
error_log( "Mailchimp Subscribe Event Received: Email: {$subscriber_email}, List ID: {$list_id}" );
// Example: If you wanted to update a custom field for a user
// $user = get_user_by_email( $subscriber_email );
// if ( $user ) {
// update_user_meta( $user->ID, 'mailchimp_subscribed', true );
// update_user_meta( $user->ID, 'mailchimp_list_id', $list_id );
// }
} else {
error_log( 'Mailchimp Subscribe Event Received with unexpected data format.' );
}
}
// Hook into the action. The dynamic part of the hook would be the event type.
// For example, if the event type is 'subscribe', the hook would be:
// 'automator_mailchimp_webhook_received_subscribe'
//
// We're assuming that the 'automator_mailchimp_webhook_received_' . $type
// part of the do_action call in the source context handles the dynamic nature.
// This example will only run if the dynamic type matched what we're expecting
// or if you specifically target a type.
// For demonstration, let's assume we want to handle the 'subscribe' event.
// You would need to know the actual event type sent by Mailchimp.
// The source code suggests the $type variable is available and used to form the hook name.
// Let's assume 'subscribe' is a possible value for $type.
add_action( 'automator_mailchimp_webhook_received_subscribe', 'my_mailchimp_subscribe_handler', 10, 1 );
// Explanation:
// This function 'my_mailchimp_subscribe_handler' is registered to run
// when the 'automator_mailchimp_webhook_received_subscribe' action is fired.
// It expects one argument: the $body, which contains the webhook data.
// The '10' is the priority, and '1' indicates that the function accepts one argument.
// The logic inside the function processes the subscriber's email address
// and logs it for demonstration purposes.
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/integrations/mailchimp/helpers/mailchimp-helpers.php:1270
public function webhook_callback( $request ) {
$body = $request->get_body_params();
$type = isset( $body['type'] ) ? $body['type'] : '';
$registered_trigger_types = array( 'unsubscribe', 'subscribe', 'upemail' );
if ( empty( $type ) ) {
$this->log( '[1/3. Event received - 400]. MailChimp has sent data but `type` is missing.' );
return;
}
if ( ! in_array( $type, $registered_trigger_types, true ) ) {
$this->log( '[1/3. Event received - 400]. Automator received webhook data from MailChimp of type: ' . $type . '. But no registered Triggers that will handle this event.' );
return;
}
$this->log( '[1/3. Event received - 200]. Automator received webhook data from MailChimp of type: ' . $type );
do_action( 'automator_mailchimp_webhook_received_' . $type, $body );
exit;
}