Passer au contenu Passer à la barre latérale

Ce sujet est résolu. Voici une description du problème et de la solution.

Problem:
You are managing multilingual options on your website with English as the default language and French as the secondary. When updating an option with the same value as the default language, the French option in the database becomes empty. This occurs due to the behavior in WPML's class-wpml-multilingual-options.php.
Solution:
This behavior is intentional for optimization in WPML. If the translated option value is identical to the original language's value, WPML deletes the translated option to avoid duplication in the database. This means that if the values are the same, WPML will not store them separately and will fall back to the original value. If you need to force the translation of admin strings or save options in different languages, consider using the following hooks:

If this solution does not apply to your case, or if it seems outdated, we recommend opening a new support ticket. Also, check related known issues at https://wpml.org/known-issues/, verify the version of the permanent fix, and confirm that you have installed the latest versions of themes and plugins. For further assistance, please visit our support forum.

Il s'agit du forum d'assistance technique de WPML, le plug-in multilingue pour WordPress.

Il est accessible à tous, toutefois seuls les clients de WPML peuvent y publier leurs messages. L'équipe du WPML répond sur le forum 6 jours par semaine, 22 heures par jour.

Marqué : 

Ce sujet contient 5 réponses, a 0 voix.

Dernière mise à jour par Ilyes Il y a 2 mois et 3 semaines.

Assisté par: Ilyes.

Auteur Messages
Avril 17, 2025 à 10:25 am #16943367

matthieuC-4

Contexte de la question:
I am trying to manage multilingual options on my website, which has English (EN) as the default language and French (FR) as the secondary language. I created an option using update_option('test', 'test value') and made it multilingual with do_action('wpml_multilingual_options', 'test'). When I update the option with a different value for FR, it works as expected. However, if I update the option with the same value as EN, the FR option becomes empty due to line 126 in class-wpml-multilingual-options.php.

Symptômes:
When updating the option with the same value as the original language, the FR option in the database becomes empty: option_name = test_fr, option_value = ''.

Questions:
Is the behavior of the FR option becoming empty when it has the same value as the original language expected?
Am I missing something in handling options with the same value as the original language?

Avril 18, 2025 à 3:14 pm #16947678

Ilyes
Supporter

Les langues: Anglais (English ) Français (Français ) Arabe (العربية )

Fuseau horaire: Pacific/Easter (GMT-06:00)

Hello,

Thank you for contacting WPML support team,

In WPML’s class-wpml-multilingual-options.php, around line 126, the logic checks:

If the new translated option value (e.g., for fr) is identical to the original language's value (EN),

Then WPML deletes the translated option (test_fr), effectively falling back to the original value (test).

This is a deliberate optimization:

If the translated value is the same as the default language’s value, there’s no need to duplicate it in the database.

You’re just seeing the internal mechanics of how WPML handles multilingual options:

- Same value as original → fallback (no need to store separately)

- Different value → stored in the DB as optionname_lang

Avril 18, 2025 à 4:11 pm #16947774

matthieuC-4

In my case the option is not deleted. Here's is a simple script :

-

update_option('test', 'test value'); // EN

do_action( 'wpml_switch_language', 'fr' ); // Switch to FR
do_action( 'wpml_multilingual_options', 'test' ); // Enable FR

update_option('test', 'test value'); // Save FR

-

I end up with an FR empty line in DB. Then when I try get_option('test'), I get an empty value back.

navicat_2025-04-18_18-10-07.png
Avril 21, 2025 à 12:20 pm #16951800

Nicolas V.
Supporter

Les langues: Anglais (English ) Français (Français )

Fuseau horaire: America/Lima (GMT-05:00)

Hi there,

If I may jump in, you might find these hooks helpful for translating options:

1. wpml_st_force_translate_admin_options
Useful for forcing the translation of admin strings.
https://wpml.org/wpml-hook/wpml_st_force_translate_admin_options/

2. wpml_multilingual_options
Possibly a better fit if you’d like to save your options in different languages.
https://wpml.org/wpml-hook/wpml_multilingual_options/

Hope this helps!

Avril 24, 2025 à 12:29 am #16962250

matthieuC-4

Hi Nicolas. Thanks for the answer.

I'm already using #2 as you can see from the code. Only it doesn't behave the way Ilyes describe, instead of avoiding the duplication in the database it does create another empty line, which will return an empty value on query of course.

I made another WPML_Multilingual_Options class which handles things the way they should. I'm confused about why I'm the only one having this problem, definitely not related to other plugins (I tested on a fresh install without plugins other than WPML), but it works now.

Avril 24, 2025 à 10:34 pm #16966472

Ilyes
Supporter

Les langues: Anglais (English ) Français (Français ) Arabe (العربية )

Fuseau horaire: Pacific/Easter (GMT-06:00)

I'm glad it is working now, could I mark this ticket as resolved?