Contact Form 7: use hook created using wpcf7_before_send_mail for only one contact form by id

半城伤御伤魂 提交于 2019-11-27 16:24:09

问题


I am working on a site with several forms created using Contact Form 7. For one of these forms, I am passing variables that I collected using a hidden input field in the form. I am passing these variables into the email using the wpcf7_before_send_mail hook, but these values are passing into every email (I added dynamic variables as well as static text) Here's 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 figure out how to only pass these values to one of the contact form email templates, probably via the form id.


回答1:


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

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

So you 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.




回答2:


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

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 have at least one unique form name in each of your forms that you can use to do this. It might still be possible to get the form ID from $contact_form via a function, but this worked and I was content with the result.



来源:https://stackoverflow.com/questions/30533216/contact-form-7-use-hook-created-using-wpcf7-before-send-mail-for-only-one-conta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!