-
-
Save senlin/4fd15ba7a19533ceb9fe to your computer and use it in GitHub Desktop.
<?php | |
/** | |
* To get this to work, you need to tinker with the acf/settings/ filter and reset the default language | |
* so that the get_field() function returns the correct results even when not on the default language. | |
* | |
* You can add the filter before you call the get_field() function and then call it again with the current | |
* language to reset it again, so it will affect other pages. | |
* | |
* answer courtesy of James of ACF Support | |
*/ | |
// add this | |
add_filter( 'acf/settings/current_language', function() { | |
global $sitepress; | |
return $sitepress->get_default_language(); | |
}); | |
// this is already in your code | |
$my_field = get_field( 'my_field', 'option' ); | |
// reset to original language | |
add_filter( 'acf/settings/current_language', function() { | |
return ICL_LANGUAGE_CODE; | |
}); | |
@graham73may the original snippet is from 2016... prolly not necessary anymore
@senlin yep, a bit surprised at the current state on this one too.
Options as of today with ACFML 2.1.3 seem to be:
A. Having to hit save on the translated options pages (I think each time the default language changes). I'm not sure I can trust end users to do / to remember to do this.
B. Or use the snippet mentioned above.
Update: Checked with WPML, and the behaviour of "Copy" is working as intended. They confirmed if the default language is updated, you then need to go into each translation and hit update (This feels super clunky!).
Thread for reference:
https://wpml.org/forums/topic/acf-wpml-acfwpml-fields-on-options-pages-set-to/
I've suggested that maybe the UI could be made a bit clearer as it currently shows the default languages value in the backend, greyed out and disabled before you've hit save.
I've also asked to submit a feature request for a setting so fields pull directly from the default language.
Probably won't go anywhere but hey-ho!
I see ACFML 2.0 has been released which has had some pretty major improvements.
I can set my field to "Copy" mode, this has the nice bonus that on the translated Options page, it shows the field as locked with this tootlip:
![image](https://private-user-images.githubusercontent.com/6642498/343064991-e018a09f-b2ab-4fff-8268-2eb120aa0faf.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjE5MzI3NjQsIm5iZiI6MTcyMTkzMjQ2NCwicGF0aCI6Ii82NjQyNDk4LzM0MzA2NDk5MS1lMDE4YTA5Zi1iMmFiLTRmZmYtODI2OC0yZWIxMjBhYTBmYWYucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDcyNSUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA3MjVUMTgzNDI0WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9MjdjNGE1YjkwM2QxYjk2MzAwOTViMGIyODRiMjVkOTllNmE5OThiYTEwMDU0MDIwZTEyNWE3NWMzMzVhMmYxZSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ._P9nEV2ysmHGLLsYR9YDvKGo1MIAEtl2TyBaQGcXNBg)
However if I use my normal code:
It is still looking for a French value, which obviously won't exist.
I've implemented @nocean's solution, but to be honest I was expecting this ACFML upgrade to understand the field is set to "Copy" mode, and to get the value from the default language.
Am I doing something wrong? Is this snippet still required?
I'm currently using this as a helper function in my theme.
EDIT: Ok this does work, but you have to save the options page in every language.
Found this thread:
https://wpml.org/forums/topic/wpml-acf-pro-options-page-default-language-values/#post-11901025
I'm not sure how I feel about this solution. If the default language's options change, you (or your client) then need to remember to go into each language's options page and hit save (even though the page only contains locked fields!).
The snippet above doesn't require that extra save process.