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

前端 未结 3 786
天涯浪人
天涯浪人 2020-12-09 18:40

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 fo

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-09 19:28

    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.

提交回复
热议问题