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.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.