Below, you’ll find a detailed guide on how to add the Translatepress Multilingual Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Translatepress Multilingual Plugin shortcodes not to show or not to work correctly.
Before starting, here is an overview of the Translatepress Multilingual Plugin and the shortcodes it provides:
"TranslatePress Multilingual is a powerful plugin that simplifies the process of creating and managing multilingual sites. It provides seamless translation for a universal audience."
- [language-switcher]
- [trp_language]
Translatepress Multilingual [language-switcher] Shortcode
‘TranslatePress Multilingual’ plugin’s ‘language-switcher’ shortcode is a powerful tool for website localization. This shortcode allows you to display a language switcher on your site. It retrieves the languages available for translation and displays them. It distinguishes between the current language and other languages, providing a seamless user experience for multi-language site visitors. It also includes options for display settings and editor access.
Shortcode: [language-switcher]
Parameters
Here is a list of all possible language-switcher shortcode parameters and attributes:
display
– Determines how the language switcher is displayed on the siteis_editor
– Specifies if the current user is an editor
Examples and Usage
Basic example – Display the language switcher without any specific parameters. This will use the default settings configured in the plugin’s settings page.
[language-switcher /]
Advanced examples
Display the language switcher with a specific display type. The ‘display’ parameter can be used to specify the type of language switcher to display. Refer to the plugin’s documentation for the possible values.
[language-switcher display=1 /]
Display the language switcher in the editor mode. The ‘is_editor’ parameter can be set to ‘true’ to display the language switcher in the editor mode. This can be useful for previewing the language switcher in the WordPress editor.
[language-switcher is_editor=true /]
Note: The exact behavior of these shortcodes may vary based on the configuration of the TranslatePress Multilingual plugin and the specific language settings of your WordPress site.
PHP Function Code
In case you have difficulties debugging what causing issues with [language-switcher]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'language-switcher', array( $this->language_switcher, 'language_switcher' ) );
Shortcode PHP function:
function language_switcher( $atts ){
ob_start();
global $TRP_LANGUAGE;
$shortcode_attributes = shortcode_atts( array(
'display' => 0,
'is_editor' => 0,
), $atts );
if ( ! $this->trp_languages ){
$trp = TRP_Translate_Press::get_trp_instance();
$this->trp_languages = $trp->get_component( 'languages' );
}
if ( current_user_can(apply_filters( 'trp_translating_capability', 'manage_options' )) ){
$languages_to_display = $this->settings['translation-languages'];
}else{
$languages_to_display = $this->settings['publish-languages'];
}
$published_languages = $this->trp_languages->get_language_names( $languages_to_display );
$current_language = array();
$other_languages = array();
foreach( $published_languages as $code => $name ) {
if( $code == $TRP_LANGUAGE ) {
$current_language['code'] = $code;
$current_language['name'] = $name;
} else {
$other_languages[$code] = $name;
}
}
$current_language = apply_filters('trp_ls_shortcode_current_language', $current_language, $published_languages, $TRP_LANGUAGE, $this->settings);
$other_languages = apply_filters('trp_ls_shortcode_other_languages', $other_languages, $published_languages, $TRP_LANGUAGE, $this->settings);
if( ! $this->trp_settings_object ) {
$trp = TRP_Translate_Press::get_trp_instance();
$this->trp_settings_object = $trp->get_component( 'settings' );
}
$ls_options = $this->trp_settings_object->get_language_switcher_options();
if ( isset( $shortcode_attributes['display'] ) && isset( $ls_options[$shortcode_attributes['display']] ) ){
$shortcode_settings = $ls_options[ $shortcode_attributes['display'] ];
}else {
$shortcode_settings = $ls_options[ $this->settings['shortcode-options'] ];
}
$is_editor = isset( $shortcode_attributes['is_editor'] ) && $shortcode_attributes['is_editor'] === 'true';
require TRP_PLUGIN_DIR . 'partials/language-switcher-shortcode.php';
return ob_get_clean();
}
Code file location:
translatepress-multilingual/translatepress-multilingual/class-translate-press.php
Translatepress Multilingual [trp_language] Shortcode
‘trp_language’ is a shortcode that enables content display in a specific language. This shortcode checks the current site language. If it matches the ‘language’ attribute, the enclosed content is displayed. If not, it outputs nothing. This allows for language-specific content on a multilingual site.
Shortcode: [trp_language]
Parameters
Here is a list of all possible null shortcode parameters and attributes:
language
– Specifies the language in which the content is displayed.
Examples and Usage
Basic example – Utilizing the shortcode to display content in a specific language. The content will only be displayed if the current language matches the one specified in the shortcode.
[trp_language language="en_US"]Hello, world![/trp_language]
Advanced examples
Using the shortcode to display content in a specific language, with nested shortcodes inside. The content and any nested shortcodes will only be processed and displayed if the current language matches the one specified in the shortcode.
[trp_language language="en_US"][contact-form-7 id="1234" title="Contact form 1"][/trp_language]
Using the shortcode with multiple instances to display different content based on the current language. Each instance of the shortcode will check the current language and display its content only if it matches the specified language.
[trp_language language="en_US"]Hello, world![/trp_language]
[trp_language language="es_ES"]¡Hola, mundo![/trp_language]
PHP Function Code
In case you have difficulties debugging what causing issues with [trp_language] shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'trp_language', 'trp_language_content');
Shortcode PHP function:
function trp_language_content( $attr, $content = null ){
global $TRP_LANGUAGE_SHORTCODE;
if (!isset($TRP_LANGUAGE_SHORTCODE)){
$TRP_LANGUAGE_SHORTCODE = array();
}
$TRP_LANGUAGE_SHORTCODE[] = $content;
extract(shortcode_atts(array(
'language' => '',
), $attr));
$current_language = get_locale();
if( $current_language == $language ){
$output = do_shortcode($content);
}else{
$output = "";
}
return $output;
}
Code file location:
translatepress-multilingual/translatepress-multilingual/includes/shortcodes.php
Conclusion
Now that you’ve learned how to embed the Translatepress Multilingual Plugin shortcodes, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.
Leave a Reply