ACF lets you register fields and field groups using PHP code. To translate these fields with WPML, you first need to make them translatable.
To programmatically register fields you need to use the acf_add_local_field_group function. Then, you can make these ACF fields translatable by specifying the wpml_cf_preferences setting for each field in your code.
There are four numerical values you can use for wpml_cf_preferences:
- Ignore: 0
- Copy: 1
- Translate: 2
- Copy once: 3
Below you can see an example of adding the wpml_cf_preferences key to a programmatically registered field group.
acf_add_local_field_group( [
'key' => 'group_1',
'title' => 'My Group',
'fields' => [
[
'key' => 'field_1',
'label' => 'Sub Title',
'name' => 'sub_title',
'type' => 'text',
'prefix' => '',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
'readonly' => 0,
'disabled' => 0,
'wpml_cf_preferences' => 2, // This field ("Sub Title") will be translated.
],
],
'location' => [
[
[
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
],
],
],
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
] );