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

前端 未结 3 784
天涯浪人
天涯浪人 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:17

    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.

提交回复
热议问题