Last updated on October 18, 2023

WPML translate widget titles

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Translate widget titles with WPML.

To translate widget titles using WPML in WordPress, you can use the wpml_register_single_string function to register the widget title for translation. Then, you can use the icl_translate function to retrieve the translated title based on the current language.

Here’s an example code snippet that demonstrates how to achieve this:

function wpsnippets_translate_widget_title($title, $widget_id) {
    // Register the widget title for translation
    wpml_register_single_string('Widget Titles', $widget_id, $title);

    // Retrieve the translated title based on the current language
    $translated_title = icl_translate('Widget Titles', $widget_id, $title);

    // Return the translated title
    return $translated_title;
}
add_filter('widget_title', 'wpsnippets_translate_widget_title', 10, 2);

In this example, the wpsnippets_translate_widget_title function is hooked into the widget_title filter. It takes the original widget title and the widget ID as parameters.

Inside the function, we first register the widget title for translation using the wpml_register_single_string function. This function takes a domain (in this case, ‘Widget Titles’), a name (the widget ID), and the string to be translated (the original widget title).

Then, we use the icl_translate function to retrieve the translated title based on the current language. This function takes the same domain, name, and string as parameters.

Finally, we return the translated title, which will be displayed instead of the original title when the widget is rendered on the front-end.

This code snippet can be useful when you want to provide translated widget titles for different languages on your WordPress site using the WPML plugin.

Examples

Example 1: Translating widget titles using WPML

This example demonstrates how to translate widget titles using the WPML plugin in WordPress. The code snippet below shows how to use the icl_translate function provided by WPML to translate a widget title based on the current language.

function wpsnippets_translate_widget_title( $title ) {
    if ( function_exists( 'icl_translate' ) ) {
        $title = icl_translate( 'Widgets', 'Widget Title', $title );
    }
    return $title;
}
add_filter( 'widget_title', 'wpsnippets_translate_widget_title' );

In this example, we define a custom function wpsnippets_translate_widget_title that takes the widget title as a parameter. We then check if the icl_translate function exists (provided by WPML) and if so, we use it to translate the widget title. Finally, we return the translated title.

Example 2: Translating specific widget titles using WPML

This example demonstrates how to translate specific widget titles using the WPML plugin in WordPress. The code snippet below shows how to use the icl_translate function to translate specific widget titles based on their unique identifiers.

function wpsnippets_translate_specific_widget_title( $title, $widget_id ) {
    if ( function_exists( 'icl_translate' ) ) {
        if ( $widget_id === 'my_widget_id' ) {
            $title = icl_translate( 'Widgets', 'Widget Title', $title );
        }
    }
    return $title;
}
add_filter( 'widget_title', 'wpsnippets_translate_specific_widget_title', 10, 2 );

In this example, we define a custom function wpsnippets_translate_specific_widget_title that takes the widget title and widget ID as parameters. We then check if the icl_translate function exists and if the widget ID matches the specific widget we want to translate. If both conditions are met, we use icl_translate to translate the widget title. Finally, we return the translated title.

Example 3: Translating widget titles with fallback using WPML

This example demonstrates how to translate widget titles with a fallback option using the WPML plugin in WordPress. The code snippet below shows how to use the icl_translate function to translate widget titles, falling back to the default title if no translation is available.

function wpsnippets_translate_widget_title_with_fallback( $title ) {
    if ( function_exists( 'icl_translate' ) ) {
        $translated_title = icl_translate( 'Widgets', 'Widget Title', $title );
        if ( $translated_title !== $title ) {
            $title = $translated_title;
        }
    }
    return $title;
}
add_filter( 'widget_title', 'wpsnippets_translate_widget_title_with_fallback' );

In this example, we define a custom function wpsnippets_translate_widget_title_with_fallback that takes the widget title as a parameter. We then check if the icl_translate function exists and use it to translate the widget title. If the translated title is different from the original title, we update the title variable with the translated title. Finally, we return the translated or fallback title.

Last updated on October 18, 2023. Originally posted on November 5, 2023.

Leave a Reply