Aleš Sýkora / February 8, 2024 / 0 comments

Fluent Forms: How to Detect and save Sender’s Website Language with Smartcode

Post summary: You can get the website language code when user submit the Fluent Form with custom Smartcode.

When you use TranslatePress plugin with your WordPress website, you can use automatic translations with DeepL or Google translation API, which is cool. TranslatePress is also compatitible with Fluent Forms form plugin.

If you want to know, if user sent mail form from specific language version of your website, you should use custom code to generate a smartcode and put it in hidden field of your form. Then, the language code will be saved when user send the form.

Prepare the smartcode for Fluent Form

Use this code in Code snippets plugin of your choice or in functions.php of your child theme. I suggest to use it after plugins_loaded hook.

<?php

// Fluent Form Site language SmartCode from great-tit.com

add_filter('fluentform/editor_shortcodes', function ($smartCodes) {
    $smartCodes[0]['shortcodes']['{site_language}'] = 'Site language';
    return $smartCodes;
});
add_filter('fluentform/all_editor_shortcodes',function($data){
	
	$customShortCodes = [
		     'title'=>'Custom',
             'shortcodes' => ['{site_language}' => 'site_language',]
		];
	$data[] = $customShortCodes;
	return $data;
	
},10,1);
add_filter('fluentform/editor_shortcode_callback_site_language', function ($value, $form) {
    $locale = get_locale();
    return $locale;
}, 10, 2);

Prepare the hidden language field in the form

After you registered the smartcode with get_locale function, you should add the hidden field to the form and use the smartcode to populate the language code. You can select the hidden field from the list in Default value input.

Optional: Add the field to the e-mail notification

If you don’t use the {all_data} in the e-mail notification, then you should add the {name_attribute} of your hidden field into the e-mail notification.

Fuel my passion for writing with a beer🍺

Your support not only makes me drunk but also greatly motivates me to continue creating content that helps. Cheers to more discoveries and shared success. 🍻

0 comments

Share Your Thoughts