Skip Navigation

Resolved

Overview of the issue

For some ACF field types it is not possible to properly duplicate content.

Workaround

If you are using any of the fields listed in this post’s Full description section, you should add this snippet to your theme’s 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(
     'test_taxonomy' => 'some_taxonomy_name',
     'test_image' => 'attachment',
     'test_file' => 'attachment',
     'test_gallery' => 'attachment',
     'test_post_object' => 'post',
     'test_page_link' => 'page'
  );

  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 );

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

Replace keys such as: ‘test_taxonomy’, ’test_image’, ’test_file’, etc. with your site’s field names, as shown in the following image example.
image01
Replace values such as: ‘some_taxonomy_name’, ‘attachment’, post, page 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.