• Resolved amandau

    (@amandau)


    Hi there,

    I am calling my contact forms using <?php echo do_shortcode(‘[contact-form-7 id=”252″]’); ?> in a template. How do I make sure it displays the correct translated contact form using a similar method inside a template?

    Thanks a lot.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Aurovrata Venet

    (@aurovrata)

    Good question Amandeau,

    basically what you need is the correct ID for your form.

    You have Polylang setup using a default language and a set of additional languages. When Polylang translates a custom post (such as the wpcf7 post), it keeps track of all translations using the post ID, by pairing the primary langauge post ID with its corresponding translated post IDs which is what the CF7 Polylang Extension does for you.

    So you need to search your translation using the primary language post ID, to do this you need to make use of the Polylang functions.

    
    $form_id = 252; //assuming this is your default language form ID
    $default_lang = pll_default_language('slug');
    $current_lang = pll_current_language('slug); //the current language your page is being viewed as
    if($current_lang != $default_lang){
      $form_id = pll_get_post($form_id, $current_lang);
      if(empty($form_id)){ //if a translation does not exists
        $form_id = 252; //show the default form
      }
    }
    //display your form
    echo do_shortcode('[contact-form-7 id=”'.$form_id.'″]');
    
    Thread Starter amandau

    (@amandau)

    That’s perfect, thank you very much! (there is just a small typo after the second slug, it’s missing an ending ‘ if someone else wanted to use this code)

    I shall make sure to give you a good review for fast support and a great plugin.

    Plugin Author Aurovrata Venet

    (@aurovrata)

    thanks, in fact I have added this question to the FAQ section for the next release.

    Thread Starter amandau

    (@amandau)

    So, a follow up question. If I wanted to do the same, but have to call the contact form from a WordPress editor (page or similar) that doesn’t accept PHP, how would I do that?

    Greatly appreciate your help.

    Plugin Author Aurovrata Venet

    (@aurovrata)

    I am not sure I fully understand.

    If you are creating a page and translating it using polylang, then you simply paste the corresponding language cf7 shortcode into each translation page.

    Thread Starter amandau

    (@amandau)

    Hi,

    No. I am using something called “Toolset” which creates layouts for pages. I’d like all languages to have the same layout. In this layout I’d also like to insert the contact form so that it is positioned in the same place on every page. Is this not possible to achieve?

    For example, I’ve inserted the WP Archive widget in this layout. When visiting the English version of the website I get the English news archive, and when visiting the Swedish version I only get the Swedish news automatically, both using the same layout with the same WP Archive widget.

    Thanks.

    Plugin Author Aurovrata Venet

    (@aurovrata)

    No. I am using something called “Toolset” which creates layouts for pages. I’d like all languages to have the same layout. In this layout I’d also like to insert the contact form so that it is positioned in the same place on every page. Is this not possible to achieve?

    Not really, page content is pulled automatically from your page ID Polylang is designed to handle translations as different pages.

    I would recommend you follow that approach, which is one of the officially recommended ways to building multilingual websites.

    For example, I’ve inserted the WP Archive widget in this layout. When visiting the English version of the website I get the English news archive, and when visiting the Swedish version I only get the Swedish news automatically, both using the same layout with the same WP Archive widget.

    Widgets are different, as the translation is handled at the widget level, not at the sidebar level.

    If you really want to achieve something similar using cf7 forms, then I would suggest you create a custom shortcode in your functions.php file where you can then handle the translation logic using the do_shortcode example provided previously.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Getting the right form using do_shortcode();’ is closed to new replies.