Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save senlin/4fd15ba7a19533ceb9fe to your computer and use it in GitHub Desktop.
Save senlin/4fd15ba7a19533ceb9fe to your computer and use it in GitHub Desktop.
How to get language independent ACF theme options on a WPML site
<?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
Copy link

graham73may commented Jun 26, 2024

@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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment