Get current language in a GravityForms field
I have a multilingual website. How can I add a field in my form to get the current language?
1. For WPML, add this code to your functions.php file
<?php /** * Get current language in Gravity Forms */ add_filter('gform_field_value_lang', 'wpster_get_language'); function wpster_get_language($value) { if (function_exists('icl_object_id')) { return ICL_LANGUAGE_CODE; } } ?>
2. For Weglot, add this code to your functions.php file
<?php /** * Get current language in Gravity Forms using Weglot */ add_filter('gform_field_value_lang', 'wpster_get_language'); function wpster_get_language($value) { if (function_exists('weglot_get_current_language')) { return weglot_get_current_language(); } } ?>
3. Add an hidden field in your Gravity Form like this
On tab “Advanced”:
- Choose “Allow field to be populated dynamically”
- Enter the field value on “Parameter Name”. For this example: lang