Last updated on October 18, 2023

WPML translate ACF custom fields

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

Translate Advanced Custom Fields (ACF) with WPML.

The code snippet below demonstrates how to translate Advanced Custom Fields (ACF) custom fields using the WPML plugin in WordPress.

function wpsnippets_translate_acf_custom_fields($value, $post_id, $field) {
    if (function_exists('icl_translate')) {
        $value = icl_translate('acf', $field['name'], $value);
    }
    return $value;
}
add_filter('acf/format_value', 'wpsnippets_translate_acf_custom_fields', 10, 3);

This code snippet adds a filter to the acf/format_value hook, which allows us to modify the value of an ACF custom field before it is displayed. Inside the filter function, we check if the WPML plugin is active by using the function_exists function. If WPML is active, we use the icl_translate function to translate the value of the custom field based on its name. Finally, we return the translated value.

This code snippet can be useful in multilingual websites where you want to translate the content of ACF custom fields using the WPML plugin. By adding this code to your theme’s functions.php file or a custom plugin, you can ensure that the custom field values are translated correctly based on the current language.

Examples

Example 1: Translating ACF custom fields using WPML

This example demonstrates how to translate ACF (Advanced Custom Fields) custom fields using the WPML (WordPress Multilingual) plugin. The code example shows how to retrieve and display translated values for ACF fields in different languages.

<?php
// Get the translated value of a specific ACF field
$translated_value = wpsnippets_get_translated_acf_field( $field_key, $post_id );

// Display the translated value
echo $translated_value;
?>

In this code example, the wpsnippets_get_translated_acf_field() function is used to retrieve the translated value of a specific ACF field. The function takes two parameters: the ACF field key and the post ID. It returns the translated value of the field for the current language. The translated value can then be displayed using echo.

Example 2: Translating ACF repeater fields using WPML

This example demonstrates how to translate ACF repeater fields using the WPML plugin. The code example shows how to retrieve and display translated values for ACF repeater fields in different languages.

<?php
// Get the translated values of a specific ACF repeater field
$translated_values = wpsnippets_get_translated_acf_repeater_field( $field_key, $post_id );

// Loop through the translated values and display them
foreach ( $translated_values as $translated_value ) {
    echo $translated_value;
}
?>

In this code example, the wpsnippets_get_translated_acf_repeater_field() function is used to retrieve the translated values of a specific ACF repeater field. The function takes two parameters: the ACF field key and the post ID. It returns an array of translated values for the field in the current language. The translated values can then be looped through and displayed using echo.

Example 3: Translating ACF flexible content fields using WPML

This example demonstrates how to translate ACF flexible content fields using the WPML plugin. The code example shows how to retrieve and display translated values for ACF flexible content fields in different languages.

<?php
// Get the translated values of a specific ACF flexible content field
$translated_values = wpsnippets_get_translated_acf_flexible_content_field( $field_key, $post_id );

// Loop through the translated values and display them
foreach ( $translated_values as $translated_value ) {
    echo $translated_value;
}
?>

In this code example, the wpsnippets_get_translated_acf_flexible_content_field() function is used to retrieve the translated values of a specific ACF flexible content field. The function takes two parameters: the ACF field key and the post ID. It returns an array of translated values for the field in the current language. The translated values can then be looped through and displayed using echo.

Last updated on October 18, 2023. Originally posted on November 6, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *