Last updated on October 18, 2023

WPML translate custom fields

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

Translate custom fields with WPML.

The code snippet below demonstrates how to translate custom fields using the WPML plugin in WordPress. This can be useful when you have custom fields that need to be translated for multilingual websites.

function wpsnippets_translate_custom_field($value, $post_id, $field_name, $language_code) {
    if (function_exists('icl_translate')) {
        $translated_value = icl_translate('custom', $field_name, $value, $language_code);
        return $translated_value;
    }
    return $value;
}

To use this code snippet, you need to pass the following parameters:

  • $value: The value of the custom field.
  • $post_id: The ID of the post or page where the custom field is attached.
  • $field_name: The name of the custom field.
  • $language_code: The language code for the translation.

The code snippet checks if the WPML plugin is active by using the function_exists() function. If WPML is active, it uses the icl_translate() function to translate the custom field value based on the provided language code. If WPML is not active, it simply returns the original value.

You can call this function whenever you need to retrieve the translated value of a custom field. For example:

$translated_title = wpsnippets_translate_custom_field(get_post_meta($post->ID, 'custom_title', true), $post->ID, 'custom_title', 'fr');

In the example above, we are retrieving the translated value of a custom field called ‘custom_title’ for the French language. The translated value will be stored in the $translated_title variable.

Examples

Example 1: Translating a Custom Field with WPML

This example demonstrates how to translate a custom field using WPML. The code retrieves the value of a custom field for the current post in the current language, and then displays it on the front-end.

$custom_field_value = get_post_meta( get_the_ID(), 'custom_field_name', true );
echo apply_filters( 'wpml_translate_single_string', $custom_field_value, 'Custom Fields', 'custom_field_name' );

The get_post_meta() function retrieves the value of the custom field for the current post. The apply_filters() function is used to translate the custom field value using WPML’s wpml_translate_single_string filter. The filter takes the custom field value, the translation domain (in this case, ‘Custom Fields’), and the name of the custom field as parameters.

Example 2: Translating Multiple Custom Fields with WPML

This example demonstrates how to translate multiple custom fields using WPML. The code retrieves the values of multiple custom fields for the current post in the current language, and then displays them on the front-end.

$custom_fields = array(
    'custom_field_1',
    'custom_field_2',
    'custom_field_3',
);

foreach ( $custom_fields as $custom_field ) {
    $custom_field_value = get_post_meta( get_the_ID(), $custom_field, true );
    echo apply_filters( 'wpml_translate_single_string', $custom_field_value, 'Custom Fields', $custom_field );
}

The code uses an array to store the names of the custom fields. It then loops through the array and retrieves the value of each custom field using get_post_meta(). The apply_filters() function is used to translate each custom field value using WPML’s wpml_translate_single_string filter.

Example 3: Translating Custom Field Labels with WPML

This example demonstrates how to translate the labels of custom fields using WPML. The code retrieves the labels of multiple custom fields and their corresponding values for the current post in the current language, and then displays them on the front-end.

$custom_fields = array(
    'custom_field_1' => 'Custom Field 1 Label',
    'custom_field_2' => 'Custom Field 2 Label',
    'custom_field_3' => 'Custom Field 3 Label',
);

foreach ( $custom_fields as $custom_field => $label ) {
    $custom_field_value = get_post_meta( get_the_ID(), $custom_field, true );
    $translated_label = apply_filters( 'wpml_translate_single_string', $label, 'Custom Fields', $custom_field . '_label' );
    echo $translated_label . ': ' . $custom_field_value;
}

The code uses an associative array to store the names of the custom fields as keys and their corresponding labels as values. It then loops through the array, retrieves the value of each custom field using get_post_meta(), and translates the label using WPML’s wpml_translate_single_string filter. The translated label is then displayed along with the custom field value.

Last updated on October 18, 2023. Originally posted on January 3, 2024.

Leave a Reply

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