Last updated on September 24, 2023

ACF translation not working

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Troubleshooting ACF Translation Issues.

ACF (Advanced Custom Fields) is a popular plugin for adding custom fields to WordPress. However, by default, ACF does not provide built-in support for translating these custom fields. To enable translation for ACF fields, you can use the pll_register_string() function provided by the Polylang plugin. This function allows you to register strings for translation.

Here’s an example code snippet that demonstrates how to enable translation for ACF fields using Polylang:

function wpsnippets_register_acf_strings() {
    if (function_exists('pll_register_string')) {
        // Register ACF field strings for translation
        pll_register_string('Field Label', get_field('field_name'), 'ACF');
    }
}
add_action('init', 'wpsnippets_register_acf_strings');

In the code snippet above, we use the pll_register_string() function to register the ACF field for translation. The first parameter is the string identifier, which can be any unique name you choose. The second parameter is the value of the ACF field, which can be retrieved using the get_field() function. The third parameter is the text domain, which is set to ‘ACF’ in this example.

By adding this code to your theme’s functions.php file or a custom plugin, you will enable translation for the specified ACF field. You can then use Polylang or any other translation plugin to translate the registered strings.

This code snippet is useful when you have ACF fields that need to be translated for multilingual websites. It allows you to easily register ACF field strings for translation using Polylang or other translation plugins.

Examples

Example 1: Translating ACF fields using WPML

This example demonstrates how to translate Advanced Custom Fields (ACF) fields using the WPML plugin. The code snippet below shows how to retrieve and display the translated value of an ACF field.

$translated_value = apply_filters( 'wpml_translate_single_string', $field_value, 'ACF', 'field_' . $field_key, $language_code );
echo $translated_value;

In this code example, we use the wpml_translate_single_string filter provided by the WPML plugin to translate the value of an ACF field. The $field_value variable represents the original value of the field, while $field_key is the unique key of the ACF field. The $language_code parameter specifies the language code for the translation. The translated value is then echoed to display it on the front-end.

Example 2: Translating ACF fields using Polylang

This example demonstrates how to translate ACF fields using the Polylang plugin. The code snippet below shows how to retrieve and display the translated value of an ACF field.

$translated_value = pll_translate_string( $field_value, $language_code );
echo $translated_value;

In this code example, we use the pll_translate_string function provided by the Polylang plugin to translate the value of an ACF field. The $field_value variable represents the original value of the field, while $language_code is the language code for the translation. The translated value is then echoed to display it on the front-end.

Example 3: Translating ACF fields using qTranslate-X

This example demonstrates how to translate ACF fields using the qTranslate-X plugin. The code snippet below shows how to retrieve and display the translated value of an ACF field.

$translated_value = qtranxf_use( $language_code, $field_value, false );
echo $translated_value;

In this code example, we use the qtranxf_use function provided by the qTranslate-X plugin to translate the value of an ACF field. The $language_code parameter specifies the language code for the translation, while $field_value represents the original value of the field. The translated value is then echoed to display it on the front-end.

Last updated on September 24, 2023. Originally posted on September 28, 2023.

Leave a Reply