I do not see an option in the Mailchimp form action to only subscribe to the Mailchimp newsletter if a checkbox is ticked. I do not want every contact form to subscribe to the newsletter, only the people who select the checkbox I have created in the Bricks form. What is the point of the Mailchimp action if you cannot conditionally subscribe if a checkbox is ticked? This option does not show up for me.
Hi @truetundra ,
I just moved this thread to feature request category.
It is not possible to have conditional action in the Bricks form.
This is already in our idea board.
Thank you.
Regards,
Jenn
Has this progressed at all? Is it now possible?
Hi James,
I’m working on a php code snippet for this at the moment. When I get it working I’ll post it here ![]()
Here is the code snippet.
You will have to replace the following values:
REPLACE_WITH_YOUR_API_KEY
REPLACE_WITH_YOUR_AUDIENCE_ID
REPLACE_WITH_YOUR_DATA_CENTER
REPLACE_WITH_YOUR_FIRST_NAME_MERGE_TAG
REPLACE_WITH_YOUR_BRICKS_FORM_ID
REPLACE_WITH_YOUR_NAME_FIELD_NAME
REPLACE_WITH_YOUR_EMAIL_FIELD_ID
REPLACE_WITH_YOUR_NEWSLETTER_FIELD_NAME
REPLACE_WITH_THE_EXACT_NEWSLETTER_OPT_IN_VALUE
/**
* Mailchimp – Bricks Contact Form Integration
*/
if ( ! defined( 'ORG_MAILCHIMP_API_KEY' ) ) {
define( 'ORG_MAILCHIMP_API_KEY', 'REPLACE_WITH_YOUR_API_KEY' );
}
if ( ! defined( 'ORG_MAILCHIMP_AUDIENCE_ID' ) ) {
define( 'ORG_MAILCHIMP_AUDIENCE_ID', 'REPLACE_WITH_YOUR_AUDIENCE_ID' );
}
if ( ! defined( 'ORG_MAILCHIMP_DATA_CENTER' ) ) {
define( 'ORG_MAILCHIMP_DATA_CENTER', 'REPLACE_WITH_YOUR_DATA_CENTER' );
}
/**
* Add or update a Mailchimp subscriber.
*
* @param string $email Subscriber email address.
* @param string $name Subscriber first name.
*
* @return true|WP_Error
*/
function org_send_contact_to_mailchimp( $email, $name = '' ) {
$email = sanitize_email( $email );
$name = sanitize_text_field( $name );
if ( ! is_email( $email ) ) {
return new WP_Error(
'invalid_email',
'The submitted email address is invalid.'
);
}
$subscriber_hash = md5(
strtolower(
trim( $email )
)
);
$url = sprintf(
'https://%s.api.mailchimp.com/3.0/lists/%s/members/%s',
ORG_MAILCHIMP_DATA_CENTER,
ORG_MAILCHIMP_AUDIENCE_ID,
$subscriber_hash
);
$body = array(
'email_address' => $email,
'status' => 'subscribed',
'status_if_new' => 'subscribed',
);
/*
* Replace this with the first-name merge tag used
* by your Mailchimp audience. FNAME is the common default.
*/
if ( '' !== $name ) {
$body['merge_fields'] = array(
'REPLACE_WITH_YOUR_FIRST_NAME_MERGE_TAG' => $name,
);
}
$response = wp_remote_request(
$url,
array(
'method' => 'PUT',
'timeout' => 15,
'headers' => array(
'Authorization' => 'Basic ' . base64_encode(
'anystring:' . ORG_MAILCHIMP_API_KEY
),
'Content-Type' => 'application/json',
),
'body' => wp_json_encode( $body ),
)
);
if ( is_wp_error( $response ) ) {
return $response;
}
$status_code = wp_remote_retrieve_response_code( $response );
$response_body = json_decode(
wp_remote_retrieve_body( $response ),
true
);
if ( $status_code < 200 || $status_code >= 300 ) {
$error_message = isset( $response_body['detail'] )
? $response_body['detail']
: 'Mailchimp returned an unknown error.';
return new WP_Error(
'mailchimp_error',
$error_message
);
}
return true;
}
/**
* Bricks Form → Mailchimp.
*
* The Bricks form must include "Custom"
* under Actions After Submit.
*/
add_action(
'bricks/form/custom_action',
function ( $form ) {
$fields = $form->get_fields();
/*
* Bricks Form element ID without "#brxe-".
*
* Example:
* If the form element ID is #brxe-bedfie,
* enter bedfie below.
*/
$target_form_id = 'REPLACE_WITH_YOUR_BRICKS_FORM_ID';
$form_id = isset( $fields['formId'] )
? sanitize_text_field( $fields['formId'] )
: '';
if ( $target_form_id !== $form_id ) {
return;
}
/*
* Replace these with the submitted Bricks field names.
*
* Example name field:
* FNAME
*
* Example email field:
* form-field-gowxcg
*
* Example newsletter checkbox field:
* subscribed
*
* Note: Although the checkbox HTML name may appear as
* subscribed[], Bricks usually provides it in the
* fields array as subscribed.
*/
$name_field_name =
'REPLACE_WITH_YOUR_NAME_FIELD_NAME';
$email_field_name =
'form-field-REPLACE_WITH_YOUR_EMAIL_FIELD_ID';
$newsletter_field_name =
'REPLACE_WITH_YOUR_NEWSLETTER_FIELD_NAME';
$name = sanitize_text_field(
$fields[ $name_field_name ] ?? ''
);
$email = sanitize_email(
$fields[ $email_field_name ] ?? ''
);
$newsletter_subscribe =
$fields[ $newsletter_field_name ] ?? array();
/*
* Bricks may provide the checkbox value as either
* an array or a string.
*/
if ( ! is_array( $newsletter_subscribe ) ) {
$newsletter_subscribe = array(
$newsletter_subscribe,
);
}
$newsletter_subscribe = array_map(
'sanitize_text_field',
$newsletter_subscribe
);
/*
* Replace this with the exact value submitted by
* the newsletter opt-in checkbox.
*
* This must match the checkbox value exactly,
* including capitalization and punctuation.
*/
$newsletter_opt_in =
'REPLACE_WITH_THE_EXACT_NEWSLETTER_OPT_IN_VALUE';
if (
! in_array(
$newsletter_opt_in,
$newsletter_subscribe,
true
)
) {
return;
}
if ( ! is_email( $email ) ) {
$form->set_result(
array(
'type' => 'danger',
'message' => 'The newsletter option was selected, but no valid email address was found.',
)
);
return;
}
$result = org_send_contact_to_mailchimp(
$email,
$name
);
if ( is_wp_error( $result ) ) {
error_log(
'Mailchimp error: ' .
$result->get_error_message()
);
$form->set_result(
array(
'type' => 'danger',
'message' => 'Your form was submitted, but the newsletter subscription could not be completed: ' .
esc_html( $result->get_error_message() ),
)
);
return;
}
/*
* Remove this block after testing if you want Bricks
* to display its regular success message.
*/
$form->set_result(
array(
'type' => 'success',
'message' => 'Your form was submitted and you were added to our newsletter.',
)
);
}
);