Skip Navigation

This thread is resolved. Here is a description of the problem and solution.

Problem:
The client needed to obtain WPML settings for custom fields using the ACF API but was unsure of the approach since no examples were provided.
Solution:
We recommended using the

get_field_object()

function to retrieve an array and then access the WPML settings with

$field['wpml_cf_preferences']

, which returns values 0, 1, 2, or 3, indicating the translation preference for the custom field. Specifically, a return value of "2" means that the custom field is set to translate.
Additionally, we provided links to documentation for hooks that can be used to obtain WPML settings:

We also shared the constants from WPML Core for reference:
https://git.onthegosystems.com/wpml/sitepress-multilingual-cms/-/blob/develop/inc/constants.php?ref_type=heads#L129-132
If the ACF API does not work as expected, we offered to provide a link to a test site with ACF and WPML for the client to recreate their approach and for us to investigate further.

Please note that this solution might be irrelevant if it's outdated or not applicable to your case. We highly recommend checking related known issues at https://wpml.org/known-issues/, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. If the issue persists, please open a new support ticket at our support forum.

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 8 replies, has 2 voices.

Last updated by Andreas W. 1 year, 3 months ago.

Assisted by: Andreas W..

Author Posts
March 6, 2024 at 6:42 pm #15381285

jean-marcd-2

Will run some tests on sandbox. I am not sure what approach the client took - no examples were provided.

Usually, get_field_object() should return an array and then we should be able to use: $field['wpml_cf_preferences'] which will return 0, 1, 2 or 3.

March 6, 2024 at 8:19 pm #15381527

Andreas W.
WPML Supporter since 12/2018

Languages: English (English ) Spanish (Español ) German (Deutsch )

Timezone: America/Lima (GMT-05:00)

Let's take an example with a text field called "test" which in ACF is set to "Translate".

To the translation preference for your ACF Field in PHP you need to use:

$my_field = get_field_object('test');
echo $my_field['wpml_cf_preferences'];

This will return "2".

There are four numerical values you can use for wpml_cf_preferences:

Ignore: 0
Copy: 1
Translate: 2
Copy once: 3

Source:
https://wpml.org/documentation/related-projects/translate-sites-built-with-acf/setting-the-translation-preferences-for-acf-fields-created-using-php-code/

March 7, 2024 at 9:42 am #15383059

jean-marcd-2

Hello Andreas,

Yes I've tried this:
$my_field = get_field_object('test');
echo $my_field['wpml_cf_preferences'];

And it works fine from the front-end if the page or post has this field attached to it. I'll get the values in that case.

But when I run the same code from within my WP-admin screen plugin (not in a post or page context) this function does not return anything. There must be a way to get this information 'wpml_cf_preferences' in the WP-admin context as the WP-admin/ACF/tools allows to export all fields as JSON with the 'wpml_cf_preferences' corresponding values.

Please let me know.

Thank you.

Best regards

JM Duchi

March 7, 2024 at 11:03 am #15383793

Andreas W.
WPML Supporter since 12/2018

Languages: English (English ) Spanish (Español ) German (Deutsch )

Timezone: America/Lima (GMT-05:00)

I am honestly not sure about this, as it is a rare use case and further custom work that is not covered by our support policy:

https://wpml.org/purchase/support-policy/

Usually, this would be the way to go:
hidden link

I would appreciate it if you could recreate your approach with a simple example on the following test site.

One-Click-Login:
hidden link

Please leave me a reply on this ticket once you have recreated the issue.

March 7, 2024 at 11:48 am #15383974

jean-marcd-2

Hello Andreas,
Thank you for your reply but I solved my problem using:
$wpml_settings = get_option('icl_sitepress_settings')['translation-management']['custom_fields_translation']['featured_video_title'];

I'm just curious to know if there is a more direct way to get this translation setting value for this ACF: 'featured_video_title'

Thank you.

Best regards.

JM Duchi

March 7, 2024 at 12:19 pm #15384143

Andreas W.
WPML Supporter since 12/2018

Languages: English (English ) Spanish (Español ) German (Deutsch )

Timezone: America/Lima (GMT-05:00)

I am glad that you found this and after reviewing the DB on a test site it appears, as if this is the only place where we save this information.

I am about to consult our second-tier support regarding a more elegant approach and will get back to you once I receive their feedback.

March 7, 2024 at 2:07 pm #15384740

Andreas W.
WPML Supporter since 12/2018

Languages: English (English ) Spanish (Español ) German (Deutsch )

Timezone: America/Lima (GMT-05:00)

Hello,

To obtain the WPML settings you can also use these hooks:

https://wpml.org/wpml-hook/wpml_setting/

https://wpml.org/wpml-hook/wpml_sub_setting/

The second-tier support further confirmed that my suggestion of using the ACF API actually should work, too:

$my_field = get_field_object('test');
echo $my_field['wpml_cf_preferences'];

// This will return "2".

2 means that the custom field is set to translate.

These are the constants from WPML Core
hidden link

In case it appears to you that the ACF API does not work as expected in this case, I can offer to send you a link to a test site with ACF and WPML where you can recreate your approach and I will have a closer look into it.

March 7, 2024 at 3:41 pm #15385456

jean-marcd-2

Ok thank you for all this information.
Just a note the ACF API does not return anything outside of Post or Page context if the ACF is attached to it.
No problem I will use:
get_option('icl_sitepress_settings')['translation-management']['custom_fields_translation']['featured_video_title'];

Thank you

JM

March 7, 2024 at 7:17 pm #15386425

Andreas W.
WPML Supporter since 12/2018

Languages: English (English ) Spanish (Español ) German (Deutsch )

Timezone: America/Lima (GMT-05:00)

Thank you for your message!

I can just provide more background about the behavior of ACF API if I know exactly which coding approach you are taking.

Anyhow, I am glad you found a workaround.

Best regards
Andreas