Contact form 7: use the hook created using wpcf7_before_send_mail for only one contact form by identifier

I am working on a site with several forms created using Contact Form 7. For one of these forms, I pass in the variables that I collected using the hidden input field on the form. I pass these variables to the email using wpcf7_before_send_mail, but these values ​​are passed to each email (I added dynamic variables as well as static text). Here is the code:

add_action( 'wpcf7_before_send_mail', 'wpcf7_add_text_to_mail_body' ); function wpcf7_add_text_to_mail_body($contact_form){ $values_list = $_POST['valsitems']; $values_str = implode(", ", $values_list); // get mail property $mail = $contact_form->prop( 'mail' ); // returns array // add content to email body $mail['body'] .= 'INDUSTRIES SELECTED'; $mail['body'] .= $values_list; // set mail property with changed value(s) $contact_form->set_properties( array( 'mail' => $mail ) ); } 

I am trying to understand how to pass these values ​​to only one of the contact form's email templates, possibly through the form identifier.

+8
source share
3 answers

Contact Form 7 uses a hidden input type to store the form identifier. It uses the name of the hidden field _wpcf7 . You can get the Id form this way.

 $form_id = $contact_form->posted_data['_wpcf7']; 

So the final code should be

 add_action( 'wpcf7_before_send_mail', 'wpcf7_add_text_to_mail_body' ); function wpcf7_add_text_to_mail_body($contact_form){ $form_id = $contact_form->posted_data['_wpcf7']; if ($form_id == 123): // 123 => Your Form ID. $values_list = $_POST['valsitems']; $values_str = implode(", ", $values_list); // get mail property $mail = $contact_form->prop( 'mail' ); // returns array // add content to email body $mail['body'] .= 'INDUSTRIES SELECTED'; $mail['body'] .= $values_list; // set mail property with changed value(s) $contact_form->set_properties( array( 'mail' => $mail ) ); endif; } 

Hope this helps.

+13
source

I used Dinesh's answer, but it stopped working for me. Instead, I am now checking a field that is unique to the form I submit:

 add_action( 'wpcf7_before_send_mail', 'wpcf7_add_text_to_mail_body' ); function wpcf7_add_text_to_mail_body($contact_form){ $submission = WPCF7_Submission::get_instance(); $posted_data = $submission->get_posted_data(); if( !empty($posted_data["dealer_email"])){ //use a field unique to your form $email = trim($posted_data["dealer_email"]); // more custom stuff here } } 

Be sure to include at least one unique form name in each of the forms that you can use to do this. Perhaps it is still possible to get the form identifier from $ contact_form via a function, but it worked, and I was pleased with the result.

+4
source

where did you put this code? I have it all written, but wherever I put it, it doesn’t work or even doesn't start ... I tried to put the code in the "advanced settings", but it didn’t work, inside the file "functions.php"? I did this and my site broke down - I don’t understand where the code is going ...

0
source

Source: https://habr.com/ru/post/988177/


All Articles