Home›Support›English Support›[Resolved] How to get translation preference in PHP from ACF Fields that are registered on the ACF UI
[Resolved] How to get translation preference in PHP from ACF Fields that are registered on the ACF UI
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:
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.
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.
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'
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.
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'];
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
Manage Cookie Consent
We use cookies to optimize our website and services. Your consent allows us to process data such as browsing behavior. Not consenting may affect some features.
Functional
Always active
Required for our website to operate and communicate correctly.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
We use these to analyze the statistics of our site. Collected information is completely anonymous.The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
These cookies track your browsing to provide ads relevant to you.