On my recent project I had to create multilingual site and we decided to add multiple languages using qTranslate plugin.
The site of course has “Contact us” page and we decided to use Contact Form 7 plugin because it’s rich and flexible plugin for creating contact forms.
So far so good, but we detected that when the language is different than the default one, the form submission is redirecting to the default language page and because the forms are different they didn’t handle properly the errors as well as the thankyou message.
The solution is very simple. Go and add this code in functions.php under your theme folder (if you don’ have such file, create it).
function wpcf7_action_url(){
if(function_exists('qtrans_convertURL')){
return qtrans_convertURL($_SERVER["REQUEST_URI"]);
}
return $_SERVER["REQUEST_URI"];
}
The short explanation: It create a filter which is applied the the Contact Form 7 action in the form tag. Using the qtrans_convertURL() the Request URI will be converted and it will use the currently selected language. I’ve added a validation if the qtrans_convertURL if some day the plugin is disabled.
Great solution, was struggling with the same problem! Thanks a lot!
thanks!!! I’m beginner in WP and this is simply and great solution 🙂
if you want to use it on a https secured connection, qtrans_convertURL($_SERVER[“REQUEST_URI”]) returns a link with “http://”. So Contact Form 7 has a wrong “action” and you will not get an Ajax response.
First you should check if there is an https secured connection:
$sendtourl = qtrans_convertURL($_SERVER[“REQUEST_URI”]);
if(isset($_SERVER[‘HTTPS’]) && ($_SERVER[‘HTTPS’] == ‘1’ || $_SERVER[‘HTTPS’] == ‘on’))$sendtourl = preg_replace(‘#^http://#’,’https://’, $sendtourl);
return $sendtourl;
Instead of of REQUEST_URI, I had to use $url . The version of cf7 I’m dealing with adds an extra “#code” at the end of the submission url…
add_filter(‘wpcf7_form_action_url’, ‘wpcf7_action_url’);
function wpcf7_action_url($url){
if(function_exists(‘qtrans_convertURL’)){
return qtrans_convertURL($url);
}
return $url;
}
How did you approach the translation of the form itself?
THX a lot! It solved my problem 🙂 – now everything is fine 🙂
Excellent post! Really saved me, I were struggling with the same issue and this solved 100%.
Did you install somme kind of extra plugin or new function in functions.php in order to define different languages to the contact form? Because I used your code but it does not work. Can you give us more details?
Also with the explanation of the code, I understood that you created as much contact forms as languages used by the website. I mean, if my website is in french and english, I have to have 2 forms, one in french the other in english. Is that right?
as the title says it’s qTranslate 🙂 as well as the code which is in the post. It’s 1 year old post and plugins which I’ve used could be changed and my changes could be insufficient.
Yes, that’s right! I’ve seen so many articles about the subject this morning…. I new your publication was interesting, and once I’ve read it again, I forgot to see the title…
But what about the contact form?
Do I have to create different in order to make your code work?
Cheers!
“… and because the forms are different they didn’t handle properly…”, yes of course they are different 🙂
ok, thanks.
Hi, I use conatct form 3.34 on WP 3.4.
I write the code in functions.php.
How and where to call the function:
add_filter(‘wpcf7_form_action_url’, ‘wpcf7_action_url’);
function wpcf7_action_url(){
if(function_exists(‘qtrans_convertURL’)){
return qtrans_convertURL($_SERVER[“REQUEST_URI”]);
}
return $_SERVER[“REQUEST_URI”];
}
in contact_form.php???
Thank you 🙂 ?
That’s what this line do:
add_filter(‘wpcf7_form_action_url’, ‘wpcf7_action_url’);
attach the function to the wpcf7 form. Bear in mind that both plugins probably evolved and the code probably is not working any more.
énorme merci pour cette astuce !!!!