Skip Navigation

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This topic contains 9 replies, has 2 voices.

Last updated by Lauren 2 months ago.

Assigned support staff: Lauren.

Author Posts
August 7, 2018 at 8:15 am #2618970

richardA-17

Hi there,

We use WPML alongside ACF for our custom fields. We have a content block builder which uses a repeater and lots of nested blocks/fields within there.

At the moment, it seems like whenever we add a new field, it is defaulting to 'Dont Translate' which is becoming a nightmare for us as as with it being in a repeater, there are thousands of variations of that field. For example: 'content_blocks_0_image__text_half_and_half' is one, with the 0 changing depending on position in the repeater.

How can we change it so that the fields default to 'Translate'? It is set to translate in the ACF group page and also the ACF group is set to translate in the WPML post type settings.

August 7, 2018 at 7:51 pm #2621663

Lauren
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Thank you for contacting WPML support. I'll be happy to help you with this.

I haven't tested this yet, but I'm wondering if you could try registering the field with a wild card symbol in the place of the number? So for example, you could set the fields to translate by adding something like this to WPML => Settings => Custom XML COnfiguration:

<custom-fields>
<custom-field action="copy">content_blocks_"*"_image__text_half_and_half</custom-field>
<custom-field action="copy">content_blocks_*_image__text_half_and_half</custom-field>
</custom-fields>

Please let me know if you test this and if it resolves the issue for you.

August 8, 2018 at 2:24 pm #2625179

richardA-17

Hi there,

Thanks for getting back to me.

So which versions should i be looking at add? The version with or without the speech marks?
Also, i'm guessing this won't impact the rest of the custom fields which use different field names?

Thanks
Will

August 8, 2018 at 2:34 pm #2625248

richardA-17

This is what i've added:

<wpml-config>
    <custom-fields>
        <custom-field action="translate">content_blocks_"*"</custom-field>
    </custom-fields>
</wpml-config>

Am i right in saying this should capture every field which starts with 'content_blocks_' ? I've updated the xml config but it hasn't updated the radio buttons in the Translation Management screen. Should it be changing those, or does the XML overwrite that?

Thanks

August 8, 2018 at 5:12 pm #2625876

Lauren
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

I was hoping this would work, but I'm thinking because it is in the middle of the name might be why it doesn't work. I also found that this has been submitted as a feature request, and I have added a vote for this ticket for it to be implemented. Ultimately the developers decide if or when a feature will be added, but there has been a good amount of request for this. I will keep you posted here as soon as I hear any more about this. In the meantime, the fields will have to be changed to "Translate" manually.

August 8, 2018 at 7:15 pm #2626166

richardA-17

Hi there,

That's disappointing. Our issue is that we have over 1000 variations of this content_blocks field due to it being a repeater and then loads of different sub-fields based on conditions etc, so changing them manually is practically impossible due to a new variation of the field basically being created each time.

Are you able to ask your developers if there is perhaps a WPML hook that we can run something on when a new field variation is created, so that we can set it to translate? As at the moment, it basically makes WPML unfeasible to use with such as large site.

Thanks

August 8, 2018 at 7:31 pm #2626200

Lauren
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

In the meantime, one of our developers has created a JS code that helps me with setting those fields.

window.acf_fix = {

  get_settings: function() {
    var self = window.acf_fix;
    self.load_jquery()
    setTimeout(self.generate_settings, 5000);
  },

  generate_settings: function() {
    var self = window.acf_fix;
    json_content = jQuery('body').text();
    parsed_json = jQuery.parseJSON( json_content );
    field_settings = [];

    for (f of parsed_json) {
      field_settings.push(f.fields);
    }

    field_settings = self.flatten(field_settings);

    output = []

    self.process_fields(output, field_settings, null);

    console.log(JSON.stringify(output));
  },

  process_fields: function(output, fields, parent_name) {
    var self = window.acf_fix;

    for (fs of fields) {
      key_name = (parent_name === null) ? fs.name : (parent_name + "_\\d+_" + fs.name);
      output.push({
        key: key_name,
        value: this.wpml_settings[fs.type]
      });

      if(fs.sub_fields) {
        self.process_fields(output, fs.sub_fields, key_name);
      }
    }
  },

  load_jquery: function(callback) {
    var jq = document.createElement('script');
    jq.src = "<em><u>hidden link</u></em>";
    document.getElementsByTagName('head')[0].appendChild(jq);
  },

  flatten: function(arr) {
    var self = window.acf_fix;
    return arr.reduce(function (flat, toFlatten) {
      return flat.concat(Array.isArray(toFlatten) ? self.flatten(toFlatten) : toFlatten);
    }, []);
  },

  // Values from WPML:
  // don't translate: 0
  // copy: 1
  // copy once: 2
  // translate: 3
  wpml_settings: {
    text: 3,
    textarea: 3,
    number: 3,
    email: 3,
    url: 3,
    password: 3,
    wysiwyg: 3,
    oembed: 3,
    image: 3,
    file: 3,
    gallery: 3,
    select: 3,
    checkbox: 3,
    radio: 1,
    true_false: 1,
    post_object: 3,
    page_link: 3,
    relationship: 3,
    taxonomy: 3,
    user: 3,
    google_map: 3,
    date_picker: 1,
    date_time_picker: 1,
    time_picker: 1,
    color_picker: 1,
    message: 3,
    tab: 1,
    repeater: 1,
    flexible_content: 1,
    clone: 1
  },

  find_fields: function(field_name) {
    return jQuery(".wpml-flex-table-cell.name").filter(function () {
      if(field_name.test(this.innerHTML)) {
        return this.innerHTML;
      }
    });
  },

  set_settings: function(input) {
    var self = window.acf_fix;

    for(field of input) {
      field_name = new RegExp("\\s+" + field.key + "\\s+");
      sys_field_name = new RegExp("\\s+\_" + field.key + "\\s+");
      
      fields = self.find_fields(field_name);
      sys_fields = self.find_fields(sys_field_name);

      jQuery(fields).each(function(index,element) {
        jQuery(element).parent('div').find('div').eq(field.value+1).find('input').click();
      });

      jQuery(sys_fields).each(function(index,element) {
        jQuery(element).parent('div').find('div').eq(2).find('input').click();
      });
    }

    console.log('Done, please save custom fields settings');
  }
}

Script usage:

1. export ACF fields definition (WordPress -> Custom fields -> Tools)
2. open exported JSON file in the browser
3. go to the dev console and paste my script
4. execute from the console the following command:
window.acf_fix.get_settings();
5. wait for the result, do not close the tab
6. in a new tab, go to the WordPress -> Settings -> Custom fields and click on the link to display hidden fields
7. when the page reloads, open dev console and paste my script
8. copy the result of 4th step and paste it in the dev console from 7th step as shown below:
input = COPIED_ARRAY_FROM_4TH_STEP;
window.acf_fix.set_settings(input);
9. when the script finish, you will see the following sentence: Done, please save custom fields settings
10. save custom fields settings

Please keep in mind that this has not been tested in other scenarios and you may need to edit it to fit your needs, this is just to be used as an example.

August 8, 2018 at 7:53 pm #2626276

richardA-17

Thanks, but again if you wouldn't mind asking your developers as whilst the JS option to resolve the fields as they are, we are just going to hit the same problem as soon as a new field variation is added.

Thanks

August 8, 2018 at 8:05 pm #2626298

Lauren
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Understood, but as of right now there is no other solution or workaround available. I will update here as soon as I hear of something else available.

July 27, 2021 at 4:55 pm #9281917

Lauren
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Closed - inactive