Contact Form 7 – Populate merge variable data with additional user information
Article Last Updated: March 4, 2021
You can populate additional Mailchimp form fields along with the email and interest groups when a contact form is submitted via Contact Form 7. See the following code snippet on how to achieve that.
Let’s say you have a field in your form for the users first name, with the id ‘your-name’ (see below):
And you had a field in your Mailchimp form called ‘Name’, and the merge tag was ‘NAME’ (see below).
Each time your Contact Form 7 form is submitted, you can populate the ‘Name’ field with the user entered data in the ‘your-name’ field of Contact Form 7.
To accomplish this, you can use the following snippet:Note: This snippet should go in the functions.php file of your active theme.
/* * Populate the 'Name' field in the Mailchimp Mailing list from the 'your-name' field in Contact Form 7 * * @since 6.0.2.1 * * @param array | $merge_variables | An array of variables we're sending to Mailchimp * @param array | $cf7_variables | An array of variables we received from the CF7 form submission * @return array| $merge_variables | An array of variables we're sending to Mailchimp, hopefully with CF7 variables added */ function additional_contact_form_7_data( $merge_variables, $cf7_variables ) { // Check our $cf7_variables array for 'your-name' if ( isset( $cf7_variables['your-name'] ) ) { // Filter name and add it to our Mailchimp merge variables array $merge_variables['NAME'] = filter_var( $cf7_variables['your-name'], FILTER_SANITIZE_STRING ); } return $merge_variables; } add_filter( 'yikes-mailchimp-contact-form-7', 'additional_contact_form_7_data', 10, 2 );
If you have additional fields you want to pre-populate, you can alter the function above as needed with additional fields to map to your Mailchimp mailing list.