Translate Customizer Text Settings With WPML
Registers chosen Customizer (theme_mod) text settings, such as a footer copyright or header phone number, in WPML String Translation and shows the translated value in each language.
Opens WPIDE on
Code
$wpide_mods = array( 'footer_text', 'header_phone' ); // theme_mod names to translate.
add_action( 'customize_save_after', function () use ( $wpide_mods ) {
foreach ( $wpide_mods as $mod ) {
$value = get_theme_mod( $mod );
if ( is_string( $value ) && '' !== $value ) {
do_action( 'wpml_register_single_string', 'Theme mods', $mod, $value );
}
}
} );
add_action( 'admin_init', function () use ( $wpide_mods ) {
if ( ! defined( 'ICL_SITEPRESS_VERSION' ) || get_transient( 'wpide_wpml_mods_registered' ) ) {
return;
}
foreach ( $wpide_mods as $mod ) {
$value = get_theme_mod( $mod );
if ( is_string( $value ) && '' !== $value ) {
do_action( 'wpml_register_single_string', 'Theme mods', $mod, $value );
}
}
set_transient( 'wpide_wpml_mods_registered', 1, DAY_IN_SECONDS );
} );
if ( ! is_admin() ) {
foreach ( $wpide_mods as $wpide_mod ) {
add_filter( 'theme_mod_' . $wpide_mod, function ( $value ) use ( $wpide_mod ) {
return is_string( $value ) && '' !== $value
? apply_filters( 'wpml_translate_single_string', $value, 'Theme mods', $wpide_mod )
: $value;
} );
}
}
Notes
List the theme_mod names in $wpide_mods (find them in your theme's customizer code). Strings refresh in String Translation whenever the Customizer is saved.
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.