Polylang: Translate Plugin and Site Text Options
Registers the text stored in selected options, such as a plugin's banner text or a footer notice, in Polylang's string translations and returns the translation on the front end.
Opens WPIDE on
Code
$wpide_options = array( 'my_plugin_notice_text' ); // Option names holding plain text.
add_action( 'init', function () use ( $wpide_options ) {
if ( ! is_admin() || ! function_exists( 'pll_register_string' ) ) {
return;
}
foreach ( $wpide_options as $option ) {
$value = get_option( $option );
if ( is_string( $value ) && '' !== $value ) {
pll_register_string( $option, $value, 'Site options', false !== strpos( $value, "\n" ) );
}
}
} );
if ( ! is_admin() ) {
foreach ( $wpide_options as $wpide_option ) {
add_filter( 'option_' . $wpide_option, function ( $value ) {
return is_string( $value ) && '' !== $value && function_exists( 'pll__' ) ? pll__( $value ) : $value;
} );
}
}
Notes
Works for options that hold a plain string. Find option names in Tools > Site Health > Info or in the plugin's settings form field names.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.