Make text saved in a WordPress option translatable, so get_option() returns the right language on the front end.
How it works #
You declare an option, and the keys inside it that hold text. Every text value found there becomes a registered string. Translate it on the Strings screen, in the group named after the plugin or theme. The code that reads the option needs no change.
Declare with wpml-config.xml #
Say a plugin saves its settings in the option my_plugin_settings:
What the option holds
my_plugin_settings = array(
'button_text' => 'Book now',
'api_key' => 'abc123',
'messages' => array( 'success' => 'Thank you!', 'error' => 'Try again.' ),
);wpml-config.xml
<wpml-config>
<admin-texts>
<key name="my_plugin_settings">
<key name="button_text" />
<key name="messages">
<key name="success" />
<key name="error" />
</key>
</key>
</admin-texts>
</wpml-config>api_key is not listed, so it is never registered or translated.
A single-value option #
When the option itself is the text, declare its name with no children:
wpml-config.xml
<admin-texts>
<key name="my_plugin_footer_note" />
</admin-texts>Wildcards #
A * in an option name is matched against the options the site actually has. Use it for options that repeat per item:
wpml-config.xml
<admin-texts>
<key name="widget_my_plugin_*">
<key name="title" />
</key>
</admin-texts>Translate the strings #
- Load a page that reads the option
Open any front-end page that uses it. The strings register the first time the option is read. - Open the Strings screen
Find the group named after your plugin or theme (for exampleplugins/my-plugin). - Translate
Type the translations. Visit the site in that language to check.
Settings screens stay safeTranslations are applied on the front end only. In wp-admin, the plugin’s own settings form always shows the stored original, so saving it never overwrites the original with a translation.
Alternative: register one string in PHP #
For a single value in your own code, skip the XML:
Register, then print
add_action( 'init', function () {
shapetranslate_register_string( 'my-plugin', 'button_text', get_option( 'my_plugin_button' ) );
} );
// Where you print it:
shapetranslate_esc_html_e( 'my-plugin', 'button_text', get_option( 'my_plugin_button' ) );Options whose names start with shapetranslate are never translated.
