Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option/function to retrieve option in a specific language? #48

Open
romanklabal opened this issue May 22, 2019 · 2 comments
Open

Option/function to retrieve option in a specific language? #48

romanklabal opened this issue May 22, 2019 · 2 comments

Comments

@romanklabal
Copy link

Hi, first of all, thank you very much for the much needed plugin that allows ACF Options to be linked with Polylang, I am happily using it on several sites already.

My question/feature request: is there a way to retrieve an option in a different language than the one currently being used on the frontend? Say I am browing the English version of the website, but I need to get an option value of a German version. I could naturally do it via WPDB, but I am hoping there is/could be another way, maybe as a param to a method, where the default value would be the currently used language?

Something along the lines if beapi_get_option($name, $language = pll_current_language()) {}

@MaximeCulea
Copy link
Contributor

Hi @rklabal,

This is a pretty good idea, thx for the request.

Something like a helper to get options value from an other language than the current one.

@hirasso
Copy link

hirasso commented Feb 26, 2024

I was able to get this to work like this:

/**
 * Get a value from an ACF options page for a specific locale
 * The locale must adhere to the WordPress standard (e.g. `en_GB` or `de_DE_formal`...)
 *
 * @param string $key           The ACF field key
 * @param string $options_id    The ACF options page's post_id
 * @param string $locale        The locale.
 * @param array $args {
 *   Optional array of arguments. Will be forwarded to ACF's get_field
 *
 *   @param boolean $format_value   Should the value be formatted?
 *   @param boolean $escape_html    Should HTML be escaped?
 * }
 */
function get_options_field_for_locale(
    string $key,
    string $options_id,
    string $locale,
    array $args = []
): mixed {
    
    $locale_filter = fn() => $locale;

    // Overwrite the current locale
    add_filter('acf/settings/current_language', $locale_filter);

    // Allow passing additional optional args to ACF's get_field()
    $args = array_merge([
        'format_value' => true,
        'escape_html' => false
    ], $args);

    $result = get_field($key, $options_id, ...$args);

    // reset the filter
    remove_filter('acf/settings/current_language', $locale_filter);

    return $result;
}

Usage

$en = get_options_field_for_locale(
    key: 'my_field',
    options_id: 'site-options',
    locale: 'en_GB',
);
$de = get_options_field_for_locale(
    key: 'my_field',
    options_id: 'site-options',
    locale: 'de_DE_formal',
);
var_dump(compact('en', 'de'));

Result

array(2) {
  ["en"] => "English Value"
  ["de"] => "Deutscher Wert"
}

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

No branches or pull requests

3 participants