Snippets

Merkulove Team Changing the Speaker multilingual locale from the ACF meta-fields

Created by Dmitry Merkulov
if (!function_exists('speaker_multilang_locale_handler')) {
    /**
     * Determines the Speaker's multilingual locale based on the Post locale and ID.
     *
     * @param string $post_locale The current post locale.
     * @param int $post_id The ID of the post.
     * @return string Multilingual locale.
     */
    function speaker_multilang_locale_handler(string $post_locale, int $post_id): string
    {

        $language_codes = array(
            'Dansk' => 'da-DK',
            'Svensk' => 'sv-SE',
            'Norsk' => 'nb-NO',
            'Engelsk' => 'en-GB',
            'Finsk' => 'fi-FI',
            'Islandsk' => 'is-IS',
        );

        // Get from ACF is available
        if (function_exists('get_field')) {
            $language_field = get_field('language', $post_id) ?? 'Dansk';
            $post_locale = $language_codes[$language_field] ?? reset($language_codes);
        }

        return $post_locale;

    }
}
add_filter('speaker_multilang_locale', 'speaker_multilang_locale_handler', 10, 2);

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.