Hi @magaworks,
I love a challenge π
I think your code looks perfect except you can’t use hyphens in your PHP variable names. Change $first-name
and $last-lastname
to $first_name
and $last_name
and everything should work.
The only issue is that you might have an extra space for people who didn’t fill out a last name. We could fix that if you’d like.
Cheers,
Kevin.
Ok, can you help me fix the extra space?
Of course.
function additional_contact_form_7_data( $merge_variables, $cf7_variables ) {
if ( isset( $cf7_variables['your-fname'] ) ) {
$first_name = filter_var( $cf7_variables['your-fname'], FILTER_SANITIZE_STRING );
$last_name = filter_var( $cf7_variables['your-lastname'], FILTER_SANITIZE_STRING );
$fullname = ! empty( $last_name ) ? $first_name . " " . $last_name : $first_name;
$merge_variables['FNAME'] = $fullname;
}
}
In this snippet, if the last name is empty, we will only add the first name (and no space). If we have a last name we’ll include the first name + a space + the last name.
Cheers,
Kevin.
Oh – make sure to return the $merge_variables
array too!
function additional_contact_form_7_data( $merge_variables, $cf7_variables ) {
if ( isset( $cf7_variables['your-fname'] ) ) {
$first_name = filter_var( $cf7_variables['your-fname'], FILTER_SANITIZE_STRING );
$last_name = filter_var( $cf7_variables['your-lastname'], FILTER_SANITIZE_STRING );
$fullname = ! empty( $last_name ) ? $first_name . " " . $last_name : $first_name;
$merge_variables['FNAME'] = $fullname;
}
return $merge_variables;
}
Something lije this:
if ( isset( $cf7_variables['your-fname'] ) ) {
$first_name = filter_var( $cf7_variables['your-fname'], FILTER_SANITIZE_STRING );
$last_lastname = filter_var( $cf7_variables['your-lastname'], FILTER_SANITIZE_STRING );
$fullname = $first_name . " " . $last_lastname;
if ( isset( $cf7_variables['your-lastname'] ) ) {
$merge_variables['FNAME'] = $fullname;
} else {
$merge_variables['FNAME'] = $first_name;
}
}
That would work but you want to check if the $cf7_variables['your-lastname']
is empty rather than if it’s set.
So this line: if ( isset( $cf7_variables['your-lastname'] ) ) {
Should be this: if ( ! empty( $cf7_variables['your-lastname'] ) ) {
I am not exactly sure how CF7 passes the data but I think $cf7_variables['your-lastname']
will be set regardless of whether the user filled it out (i.e. it’s set but it’s empty).
You win. Your code is cleaner.
But I am not a developer. I am just a fast learner and curious, self-taught.
I think you’re doing great and I only want to help π
Something is not working. It is not sending to mailchimp, althought the Contact Form is sent.
I have Contact form 7 integration on and select the Newsletter list.
Here is my final code:
add_filter( 'yikes-mailchimp-contact-form-7', 'additional_contact_form_7_data', 10, 2 );
function additional_contact_form_7_data( $merge_variables, $cf7_variables ) {
if ( isset( $cf7_variables['your-fname'] ) ) {
$first_name = filter_var( $cf7_variables['your-fname'], FILTER_SANITIZE_STRING );
$last_name = filter_var( $cf7_variables['your-lname'], FILTER_SANITIZE_STRING );
$fullname = ! empty( $last_name ) ? $first_name . " " . $last_name : $first_name;
$merge_variables['FNAME'] = $fullname;
}
if ( isset( $cf7_variables['company'] ) ) {
$merge_variables['COMPANY'] = filter_var( $cf7_variables['company'], FILTER_SANITIZE_STRING );
}
if ( isset( $cf7_variables['origem-lp'] ) ) {
$merge_variables['ORIGEM'] = filter_var( $cf7_variables['origem-lp'], FILTER_SANITIZE_STRING );
}
if ( isset( $cf7_variables['TOS-hiden'] ) ) {
$merge_variables['TOS'] = filter_var( $cf7_variables['TOS-hiden'], FILTER_SANITIZE_STRING );
}
return $merge_variables;
}
Can you enable debugging in the plugin’s settings and let me know if there’s an error showing there?
Do you have the MERGE fields COMPANY
, ORIGEM
, and TOS
in your Mailchimp list?
I have debug enabled and nothing is showing there.
And yes I have those MERGE Fields in Mailchimp.
No fields are mandatory except email.
Are you using our filter to make the integration single opt-in? If not, the subscription will be double opt-in. If you’re using a test email address, the double opt-in will never be confirmed. Could that be the problem?
Yes, I have this code:
add_filter( 'yikes-mailchimp-checkbox-integration-body', 'set_integrations_to_single_optin', 10, 2 );
function set_integrations_to_single_optin( $request_body, $integration_type ) {
$request_body['status_if_new'] = 'subscribed';
$request_body['status'] = 'subscribed';
return $request_body;
}
Just before the other one.