• Resolved marcelograciano

    (@marcelograciano)


    I have a website with 3 form, but only two of these forms must enter information into the Salesforce database. How do I define which forms should use the hook and which shall without going through it?

    Follow the code I’m using:

    
        add_action( 'wpcf7_before_send_mail', 'my_conversion' );
          function my_conversion( $contact_form ) {
            $title = $contact_form->title;
            $submission = WPCF7_Submission::get_instance();
        
            if ( $submission ) {
                $posted_data = $submission->get_posted_data();
          }
        
              $email = $posted_data["your-email"];
              $name  = $posted_data["your-name"];
              $last  = $posted_data["your-lastname"];
              $phone = $posted_data["tel-71"];
              $description = $posted_data["your-message"];
        
        
                $post_items[] = 'oid=00D4000000xxxxx';
                $post_items[] = 'first_name=' . $name;
                $post_items[] = 'last_name=' . $last;
                $post_items[] = 'company=';
                $post_items[] = 'email=' . $email;
                $post_items[] = 'phone=' . $phone;  
                $post_items[] = 'description=' . $description;
                $post_items[] = '00N4000000XXXX=' . "Shopping";
                $post_items[] = '00N4000000xxxxxx=' . "1";
                $post_items[] = 'external=1';
                $post_items[] = 'lead_source=Shopping';
        
            $post_string = implode ('&', $post_items);
        
            $ch = curl_init( 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8' );
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1 );
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
            curl_exec($ch); // Post to Salesforce
            curl_close($ch); // close cURL resource
        }

    Thank you for your help.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Takayuki Miyoshi

    (@takayukister)

    Try checking the result of $contact_form->id() or $contact_form->title().

    Thread Starter marcelograciano

    (@marcelograciano)

    Thank you for the tip. I had found this function:

    if ( $contact_form->id !== $myform_id )
            return;

    But I know very little about PHP. Where should I enter this line? Would you help me?

    Thanks again.

    Thread Starter marcelograciano

    (@marcelograciano)

    I did like this:

    add_action( 'wpcf7_before_send_mail', 'my_conversion' );
      function my_conversion( $contact_form ) {
        $title = $contact_form->title;
        $submission = WPCF7_Submission::get_instance();
        $id = $contact_form->id();
    	
    	if ( $id==68 ) {
           $submission->skip_mail = true;
      }
    	
       if ( $id==65 ) {
            $posted_data = $submission->get_posted_data();
      }
    
          $email = $posted_data["your-email"];
          $name  = $posted_data["your-name"];
    	  $last  = $posted_data["your-lastname"];
          $phone = $posted_data["tel-244"];
    	  $description = $posted_data["your-message"];
    	    
    
            $post_items[] = 'oid=00D41000000dC7r';
            $post_items[] = 'first_name=' . $name;
            $post_items[] = 'last_name=' . $last;
    	    $post_items[] = 'company=';
            $post_items[] = 'email=' . $email;
            $post_items[] = 'phone=' . $phone;  
    	    $post_items[] = 'description=' . $description;
            $post_items[] = 'external=1';
            $post_items[] = 'lead_source=Radikal';
    
        $post_string = implode ('&', $post_items);
    
        $ch = curl_init( 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8' );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1 );
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_exec($ch); // Post to Salesforce
        curl_close($ch); // close cURL resource
    }

    Now the form of ID = 65 normally recorded in Salesforce. But the ID = 68 does not send the email, is locked with the fields filled. What am I doing wrong? Does anyone have any ideas?

    Thanks.

    Thread Starter marcelograciano

    (@marcelograciano)

    Hello. I got a solution, I do not think it’s the best but here is what I did:

    add_action( 'wpcf7_before_send_mail', 'my_conversion' );
      function my_conversion( $contact_form ) {
        $title = $contact_form->title;
        $submission = WPCF7_Submission::get_instance();
    
        if ( $submission ) {
            $posted_data = $submission->get_posted_data();
      }
      
       if ( 'FORM NAME' == $title ) {
      
              $email = $posted_data["your-email"];
              $name  = $posted_data["your-name"];
              $last  = $posted_data["your-lastname"];
              $phone = $posted_data["tel-71"];
              $description = $posted_data["your-message"];
        
        
                $post_items[] = 'oid=00D4000000xxxxx';
                $post_items[] = 'first_name=' . $name;
                $post_items[] = 'last_name=' . $last;
                $post_items[] = 'company=';
                $post_items[] = 'email=' . $email;
                $post_items[] = 'phone=' . $phone;  
                $post_items[] = 'description=' . $description;
                $post_items[] = '00N4000000XXXX=' . "Shopping";
                $post_items[] = '00N4000000xxxxxx=' . "1";
                $post_items[] = 'external=1';
                $post_items[] = 'lead_source=Shopping';
        
            $post_string = implode ('&', $post_items);
        
            $ch = curl_init( 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8' );
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1 );
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
            curl_exec($ch); // Post to Salesforce
            curl_close($ch); // close cURL resource
     }

    I used the solution suggested by @takayukister. In case “$contact_form->title()”.

    Just remember to change “FORM NAME” with the name of the form that you want to perform the action.

    Thanks again @takayukister!

    Thank You! This is first solution that works for me but you have lost } at the end.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Limit Contact Form 7 hook (Before Send) to specific form’ is closed to new replies.