Polylang Translations for Customizer Settings
Registers chosen Customizer text settings, such as footer copyright or a header phone number, in Polylang's string translations and shows the right translation in each language.
Opens WPIDE on
Code
$wpide_mods = array( 'footer_text', 'header_phone' ); // theme_mod names to translate.
add_action( 'init', function () use ( $wpide_mods ) {
if ( ! is_admin() || ! function_exists( 'pll_register_string' ) ) {
return;
}
foreach ( $wpide_mods as $mod ) {
$value = get_theme_mod( $mod );
if ( is_string( $value ) && '' !== $value ) {
pll_register_string( $mod, $value, 'Theme mods', false !== strpos( $value, "\n" ) );
}
}
} );
if ( ! is_admin() ) {
foreach ( $wpide_mods as $wpide_mod ) {
add_filter( 'theme_mod_' . $wpide_mod, function ( $value ) {
return is_string( $value ) && '' !== $value && function_exists( 'pll__' ) ? pll__( $value ) : $value;
} );
}
}
Notes
List the theme_mod names in $wpide_mods, save the Customizer, then translate them in Languages > Translations under "Theme mods".
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.