ACF and WPML: Global Options values instead of different values for each language

Using Advanced Custom Fields and WPML, retrieve a global Option value regardless of the current active language.


When using the powerful Advanced Custom Fields plugin in tandem with WPML, you will find that the value returned by the get_field('field_name', 'option') function will differ depending upon the current active language.

This means that if you view the corresponding Options page of the CMS in English, you’ll be able to add different values for each of the fields that display there than if you choose to view the same page in Spanish or French. This is perfect if you want to have different values across each language version of your site, but there may be occasions where you want to share values across languages without having to duplicate the data in the CMS.

The following code snippet creates a useful helper function called get_global_option() which will always return the value of an option field from the default language of the site, rather than whichever language is currently being viewed. Just copy and paste the following snippet into your functions.php file, or equivalent:


/**
 * ACF / WPML Options
 * Return the value of an Options field from WPML's default language
 */

function barebones_acf_set_language() {
  return acf_get_setting('default_language');
}
 
function get_global_option($name) {
    add_filter('acf/settings/current_language', 'barebones_acf_set_language', 100);
    $option = get_field($name, 'option');
    remove_filter('acf/settings/current_language', 'barebones_acf_set_language', 100);
    return $option;
}

You can then retrieve global option values within your WordPress theme using the syntax below:

get_global_option('option_name')

A lightweight, intuitive WordPress theme to enable flexible developement.

  • Lighting-fast installer
  • Intuitive SASS structure
  • Bloat-free

Build with Barebones