1

I am trying to send contact us form details to Insightly CRM with Web to Lead html code. I changed action url of the form with the following code in my functions.php:

add_filter('wpcf7_form_action_url', 'wpcf7_custom_form_action_url');
function wpcf7_custom_form_action_url($url) {
  global $post;
  $id_to_change = 1315;
  if($post->ID === $id_to_change)
    return 'https://xxxx.insight.ly/WebToLead/Create';
  else
    return $url;
}

Everything looks fine on inspector but i get the following error on submit:

XMLHttpRequest cannot load https://xxxx.insight.ly/WebToLead/Create. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://xxxx.com' is therefore not allowed access.

I tried adding this to my functions.php:

add_action( 'init', 'allow_origin' );
function allow_origin() {
    header("Access-Control-Allow-Origin: *");
}

I tried adding this in theme.php:

 header("Access-Control-Allow-Origin: *");

I tried adding this to scripts.js of contact form 7 plugin:

$.ajax({
   url: url,
   ++headers: { 'Access-Control-Allow-Origin': '*' },
   ++crossDomain: true,

I tried adding this to .htaccess:

Header always set Access-Control-Allow-Origin "*"

Nothing works :( My server has Varnish 4 and ConfigServer Security & Firewall but i disabled both and still get the same error. Please help me :(

Grokify
  • 15,092
  • 6
  • 60
  • 81
Alex Bogias
  • 1,826
  • 2
  • 30
  • 45

1 Answers1

1

After my research I noticed that javascript was the problem and it was not possible to bypass it with the Access-Control-Allow-Origin in any way on my side.

I used curl in a hooked php script so it can send the details to the other domain.

So I added a hook in my functions.php which I am overriding the wpcf7_mail_sent function:

add_filter('wpcf7_mail_sent', 'wpcf7_custom_mail_sent');
function wpcf7_custom_mail_sent($contact_form) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL,"https://xxxx.insight.ly/WebToLead/Create");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($_REQUEST));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    curl_exec($ch);
    curl_close($ch);
}
Grokify
  • 15,092
  • 6
  • 60
  • 81
Alex Bogias
  • 1,826
  • 2
  • 30
  • 45