Last active
March 13, 2024 16:00
-
-
Save senlin/4fd15ba7a19533ceb9fe to your computer and use it in GitHub Desktop.
How to get language independent ACF theme options on a WPML site
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
}); | |
Nice gist, in particular the solution by @peterblickenstorfer (Backend/Admin Code) works for me. I just needed to group the really global option fields to a separate field group and option page.
It does makes a lot of sense to use the "All Languages" instead of setting the default language custom fields.
Update:
I have posted too soon without checking if things are working properly. There is just something wrong when I used add_filter('acf/settings/current_language', function() { return 'all' ; } );
. It might be my WPML setup?
So I have reverted back to using the "default language" field as a global field, and used the Backend/Admin Code by @peterblickenstorfer
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi and thanks for the code-snippets.
Because there is no way to handle this smart in the WP-Backend (see the comment of @elliottmangham) I use the "language=all"-soloution with a force-redirect in the backend by admin_init-action.
Template-Code:
Backend- / Admin-Code (functions.php):