Translatable Text Shortcode for WPML
Adds a [wpide_t name="..."]Text[/wpide_t] shortcode that registers its text in WPML String Translation and shows the translation, ideal for short phrases in widgets, footers and page builder text.
Opens WPIDE on
Code
add_shortcode( 'wpide_t', function ( $atts, $content = '' ) {
$atts = shortcode_atts( array( 'name' => '' ), $atts, 'wpide_t' );
$name = sanitize_text_field( $atts['name'] );
$text = trim( (string) $content );
if ( '' === $name || '' === $text || ! defined( 'ICL_SITEPRESS_VERSION' ) ) {
return do_shortcode( $text );
}
if ( current_user_can( 'manage_options' ) ) {
do_action( 'wpml_register_single_string', 'Shortcode text', $name, $text );
}
return do_shortcode( (string) apply_filters( 'wpml_translate_single_string', $text, 'Shortcode text', $name ) );
} );
Notes
Give every phrase a unique name. Strings register the first time an administrator views a page that uses them.
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.