Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Contact Form 7 Form ID In PHP

I'm using contact form 7 to load two different forms into a page and then, in addition to sending the email, dynamically adding that information to a database. Unfortunately, because of the plugin, I can't simply just create all inputs with different names to avoid needing a filter. So, essentially, I'd like to pull the form ID into the action hook and dynamically create the $data variable based on which form is being submitted, but I'm not sure how to get the cf7 form ID. Does anyone know how to accomplish this, or perhaps a more feasible way of doing it?

Form Shortcodes

[contact-form-7 id="221" title="Reg 1"] [contact-form-7 id="112" title="Reg 2"]

PHP Action Hook in functions.php

function save_form( $wpcf7 ) {


global $wpdb;

   $form_to_DB = WPCF7_Submission::get_instance();

   if($form_to_DB) {
       $formData = $form_to_DB->get_posted_data(); 
   }

   if("Request a Free Demo" != $formData['demo_request'][0]){
   $freeDemo = "yes";}else { $freeDemo = "nope";}

   if(THE FORM ID = 221) {
   $data = array(
          some values from the 112 form
      $wpdb->insert( $wpdb->prefix . 'registrations', $data );
   );
   }elseif(THE FORM ID = 112) {
     $data = array(
          some other values from the 112 form
   $wpdb->insert( $wpdb->prefix . 'registrations_2', $data );
   );
   }



}
remove_all_filters('wpcf7_before_send_mail');
add_action('wpcf7_before_send_mail', 'save_form' );
like image 767
Jon Hendershot Avatar asked Sep 22 '15 03:09

Jon Hendershot


3 Answers

I tend to use the "wpcf7_posted_data" action hook (though this might have changed as the question is a bit old now). You don't need to remove all filters.

For example:

function processForm($cf7)
{
    $wpcf7 = WPCF7_ContactForm::get_current();

    $form_id = $wpcf7->id;

    if ($form_id === 221)
    {
        //Do Stuff
    }

    else if ($form_id === 112)
    {
        //Do different stuff
    }
}

add_action("wpcf7_posted_data", "processForm");
like image 83
Ryan Konkolewski Avatar answered Oct 28 '22 16:10

Ryan Konkolewski


$wpcf7->idis no longer accessible, use $wpcf7->id() instead.

like image 43
frederikvdbe Avatar answered Oct 28 '22 17:10

frederikvdbe


simply use this:

function save_form( $wpcf7 ) {
    if($wpcf7->id == 4711) {
        // whatever
    }
}
like image 23
Andreas Schmidt Avatar answered Oct 28 '22 18:10

Andreas Schmidt