Skip Navigation

Resolved

Reported for: WPML Multilingual CMS 3.3.6

Overview of the issue

Repeater add-on provides a repeater field type that is designed to contain other field types. For user to be able to duplicate content within a repeater, it is necessary to add a snippet and carefully add field names like described in the Workaround section.

This issue currently exists for the following field types:
– image
– file
– gallery
– taxonomy
– post object
– page link

NOTE: You can use the provided code snippet for all other field types. The only difference is in the name of the field. This means that you can use this snippet to resolve issues with duplication of both repeater and regular field types.

You can find the errata for these field types here

Workaround

You can use this snippet in your functions.php file, in order for duplication to work properly:

function wpml_fix_acf_duplication( $value_to_filter, $target_language, $meta_data ) {
  // Field names should be placed into following array.
  $fields = array(
     'taxonomy_field' => 'some_taxonomy_name',
     'repeater_field_0_repeater_taxonomy_field' => 'some_other_taxonomy_name',
     'repeater_field_1_repeater_taxonomy_field' => 'some_other_taxonomy_name',
     'repeater_field_0_repeater_child_0_repeater_child_taxonomy_field' => 'some_other_taxonomy_name',
     'repeater_field_0_repeater_child_1_repeater_child_taxonomy_field' => 'some_other_taxonomy_name',
     'repeater_field_1_repeater_child_0_repeater_child_taxonomy_field' => 'some_other_taxonomy_name',
     'repeater_field_1_repeater_child_1_repeater_child_taxonomy_field' => 'some_other_taxonomy_name'
  );


  if ( array_key_exists( $meta_data['key'], $fields ) ) {
		$value = maybe_unserialize( $value_to_filter );

		if ( is_array( $value ) ) {
			// If custom field value contain array of IDs to translate, usually used for gallery and taxonomy field types.
			$translated_ids = array();

			foreach ( $value as $v ) {
				$translated_ids[] = apply_filters( 'wpml_object_id', $v, $fields[ $meta_data['key'] ], true, $target_language );
			}

			return maybe_serialize( $translated_ids );
		} else {
			// If custom field value contain single ID to translate, usually used for image and file field types.
			$translated_id = apply_filters( 'wpml_object_id', $value, $fields[ $meta_data['key'] ], true, $target_language );

			return $translated_id;
		}
	}

	return $value_to_filter;
}
add_filter( 'wpml_duplicate_generic_string', 'wpml_fix_acf_duplication', 999, 3 );

Please note that you should replace (‘taxonomy_field’, ‘repeater_field_0_repeater_taxonomy_field’, etc.) field names with the ones in your site. Use the following steps to do this.

In the above example, we have the following in our field group:

– One taxonomy field

– One repeater field with a taxonomy field and another “child” repeater field with its own taxonomy field.

This mean that the nesting look like this:

Taxonomy
Repeater {
         Taxonomy
         Repeater Child { 
                        Taxonomy 
         }
}

This means you need to set your fields according to your needs, like we did in the example shown in the following image.

image04

When you edit your post, page or whichever post type you are using your custom fields with, you will see those fields in action. Following image shows how this looks in our example:

image03

NOTE: You should replace array key =>; value pairs with proper values.

Replace array keys:

If you carefully look at our field names as keys, you will notice that fields are indexed from 0.

That means your first repeater is 0, second is 1 and so on. Those repeater numbers are separated with underscores from other child field types with _0_, _1_, _2_

For example, the name of taxonomy field within our first repeater is repeater_field_0_repeater_taxonomy_field, and that is the name we will put in snippet array above. Make sure you focus on three things:

  1. Inside an array, as keys, put every field you are using and make sure it is of one types listed below
  2. Name of the field is properly formatted
  3. Name of the field is added for every repeater row separately, i.e. if you have 10 repeater rows names, it will be as follow: repeater_field_0_some_custom_field, repeater_field_1_some_custom_field, repeater_field_2_some_custom_field… all the way to repeater_field_9_some_custom_field.

Additionally, you can find all the field names present in “Multilingual Content Setup” if you look underneath your Advanced Custom Fields fields and WYSIWYG editor after your post is saved, as shown in the following image.

image05

Replace values such as: ‘some_taxonomy_name’,  ‘some_other_taxonomy_name’ etc. accordingly. This is the type of element the ID belongs to. Can be post, page, attachment, {custom post type key}, nav_menu, nav_menu_item, category, post_tag, {custom taxonomy key}

For more please check out our hook documentation here.

4 Respostas para “Advanced Custom Fields (ACF) repeater duplication issue”

  1. It does’t works for me in that way. It works if fields are set to “Translate” instead of “Do nothing”. Any way thanks for temporary solution. Spend a lot of time to figure it out, thought it was my mistake somewhere.

    This is the type of element the ID belongs to

    I have a question what if field is attached to several post types?

    • Hello Julio

      we are currently working on new solution for this issue in ACF. It will be probably released as separate plugin or integrated directly in WPML so no snippet will be necessary

        • Hi, this is supposed to be resolved already using our ACF add-on – “Advanced Custom Fields Multilingual”, if you are using the add-on and you have all the other plugins versions up to date, please open a support ticket and we’ll debug your issue. I am also passing this information to our dev team so they can run some tests.

          Thanks!