Support

Account

Home Forums General Issues Translating ACF form field labels in frontend with WPML

Unread

Translating ACF form field labels in frontend with WPML

  • A follow-up for this forum post about translating form field labels with Polylang. This time with WPML String Translation.

    add_action('init', function(){
        if (!is_admin()) {
            foreach (acf_get_field_groups() as $group) {
                $fields = acf_get_fields($group['ID']);
                if (is_array($fields) && count($fields)) {
                    foreach ($fields as $field) {
                        do_action('wpml_register_single_string', 'ACF Labels', $field['label'], $field['label']);
                        do_action('wpml_register_single_string', 'ACF Messages', $field['message'], $field['message']);
                        do_action('wpml_register_single_string', 'ACF Instructions', $field['instructions'], $field['instructions']);
                    }
                }
            }
        }
    });
    
    add_filter('acf/load_field', function (array $field) {
    	$field['label'] = apply_filters('wpml_translate_single_string', $field['label'], 'ACF Labels', $field['label']);
    	$field['message'] = apply_filters('wpml_translate_single_string', $field['message'], 'ACF Messages', $field['message']);
    	$field['instructions'] = apply_filters('wpml_translate_single_string', $field['instructions'], 'ACF Instructions', $field['instructions']);
    	return $field;
    });
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.