Last updated on October 18, 2023

WPML translate breadcrumbs

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

Translate and customize breadcrumbs with WPML.

The code snippet below demonstrates how to translate breadcrumbs using WPML in WordPress. This can be useful when you have a multilingual website and want to display translated breadcrumbs based on the current language.

function wpsnippets_translate_breadcrumbs($breadcrumbs) {
    if (function_exists('icl_get_languages')) {
        $languages = icl_get_languages('skip_missing=0');
        $current_language = ICL_LANGUAGE_CODE;

        foreach ($breadcrumbs as &$breadcrumb) {
            if (isset($breadcrumb['url'])) {
                $translated_url = icl_get_home_url($languages[$current_language]['language_code']);
                $translated_url .= substr($breadcrumb['url'], strlen(home_url()));
                $breadcrumb['url'] = $translated_url;
            }
        }
    }

    return $breadcrumbs;
}
add_filter('wpseo_breadcrumb_links', 'wpsnippets_translate_breadcrumbs');

This code snippet uses the wpseo_breadcrumb_links filter hook provided by the Yoast SEO plugin to modify the breadcrumbs. It checks if the WPML plugin is active by using the icl_get_languages function. If WPML is active, it retrieves the available languages and the current language code.

Then, it loops through each breadcrumb item and checks if it has a URL. If a URL is found, it translates the URL using the icl_get_home_url function and replaces the original URL with the translated URL.

Finally, the modified breadcrumbs are returned using the wpsnippets_translate_breadcrumbs function, which is hooked into the wpseo_breadcrumb_links filter.

By using this code snippet, you can ensure that the breadcrumbs on your multilingual website are properly translated based on the current language.

Examples

Example 1: Translating Breadcrumbs using WPML

This example demonstrates how to translate breadcrumbs using WPML, a popular WordPress plugin for multilingual websites. The code snippet below shows how to retrieve the breadcrumbs and translate them using the wpml_translate_string() function provided by WPML.

function wpsnippets_translate_breadcrumbs() {
    if (function_exists('icl_object_id')) {
        $breadcrumbs = get_breadcrumbs(); // Retrieve the breadcrumbs
        $translated_breadcrumbs = array();

        foreach ($breadcrumbs as $breadcrumb) {
            $translated_breadcrumbs[] = wpml_translate_string('breadcrumb', $breadcrumb); // Translate each breadcrumb
        }

        return $translated_breadcrumbs;
    }

    return false;
}

Explanation:

  • The wpsnippets_translate_breadcrumbs() function checks if the WPML plugin is active using the function_exists() function.
  • It then retrieves the breadcrumbs using the get_breadcrumbs() function (custom function).
  • Each breadcrumb is translated using the wpml_translate_string() function, passing the string context (‘breadcrumb’) and the breadcrumb text as arguments.
  • The translated breadcrumbs are stored in an array and returned.

Example 2: Translating Breadcrumbs with Language Switcher

This example demonstrates how to translate breadcrumbs dynamically based on the selected language using WPML’s language switcher. The code snippet below shows how to modify the previous example to include a language switcher and update the breadcrumbs accordingly.

function wpsnippets_translate_breadcrumbs_with_switcher() {
    if (function_exists('icl_object_id')) {
        $breadcrumbs = get_breadcrumbs(); // Retrieve the breadcrumbs
        $translated_breadcrumbs = array();

        foreach ($breadcrumbs as $breadcrumb) {
            $translated_breadcrumbs[] = wpml_translate_string('breadcrumb', $breadcrumb); // Translate each breadcrumb
        }

        $language_switcher = icl_get_languages('skip_missing=0&orderby=code'); // Get the language switcher

        foreach ($language_switcher as $language) {
            $language_breadcrumbs = array();

            foreach ($translated_breadcrumbs as $breadcrumb) {
                $language_breadcrumbs[] = wpml_translate_string('breadcrumb', $breadcrumb, array('language' => $language['language_code'])); // Translate each breadcrumb for the current language
            }

            $translated_breadcrumbs[$language['language_code']] = $language_breadcrumbs; // Store the translated breadcrumbs for each language
        }

        return $translated_breadcrumbs;
    }

    return false;
}

Explanation:

  • The wpsnippets_translate_breadcrumbs_with_switcher() function is similar to the previous example, but it includes an additional step to translate the breadcrumbs for each language.
  • It retrieves the breadcrumbs and translates them as before.
  • It then retrieves the language switcher using the icl_get_languages() function, which returns an array of available languages.
  • For each language, it creates a new array to store the translated breadcrumbs.
  • It loops through the translated breadcrumbs and translates each breadcrumb for the current language using the wpml_translate_string() function, passing the language code as an additional argument.
  • The translated breadcrumbs for each language are stored in the $translated_breadcrumbs array using the language code as the key.

Example 3: Translating Breadcrumbs with Custom Language Switcher

This example demonstrates how to translate breadcrumbs using a custom language switcher instead of WPML’s built-in language switcher. The code snippet below shows how to modify the previous example to use a custom language switcher and update the breadcrumbs accordingly.

function wpsnippets_translate_breadcrumbs_with_custom_switcher($language_code) {
    if (function_exists('icl_object_id')) {
        $breadcrumbs = get_breadcrumbs(); // Retrieve the breadcrumbs
        $translated_breadcrumbs = array();

        foreach ($breadcrumbs as $breadcrumb) {
            $translated_breadcrumbs[] = wpml_translate_string('breadcrumb', $breadcrumb, array('language' => $language_code)); // Translate each breadcrumb for the specified language
        }

        return $translated_breadcrumbs;
    }

    return false;
}

Explanation:

  • The wpsnippets_translate_breadcrumbs_with_custom_switcher() function is a simplified version of the previous example, as it assumes that the language code is passed as an argument.
  • It retrieves the breadcrumbs and translates them for the specified language using the wpml_translate_string() function, passing the language code as an additional argument.
  • The translated breadcrumbs are stored in an array and returned.
Last updated on October 18, 2023. Originally posted on January 18, 2024.

Leave a Reply

Your email address will not be published. Required fields are marked *