Skip Navigation

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

Elementor users - please update WPML to the latest version to maintain compatibility. More details here - https://wpml.org/changelog/2024/12/wpml-4-6-15-critical-update-for-elementor-sites/

This topic contains 2 replies, has 1 voice.

Last updated by Prosenjit Barman 1 week, 5 days ago.

Author Posts
December 19, 2024 at 2:30 pm #16530554

pimv-12

Background of the issue:
I am trying to make a custom language switcher work on my WordPress site using WPML. I have created a custom shortcode for a language switcher with country codes and a '/' separator. The code is as follows: // Create a custom shortcode for a language switcher with country codes and '/' separator function custom_language_switcher_shortcode() { if (function_exists('icl_get_languages')) { global $wp; $current_url = home_url(add_query_arg(array(), $wp->request)); $languages = icl_get_languages('skip_missing=0&orderby=code'); if (!empty($languages)) { $output = '

'; $count = 0; $total_languages = count($languages); foreach ($languages as $lang_code => $language) { // Get the translated URL for the current page $translated_url = apply_filters('wpml_permalink', $current_url, $lang_code); if (!$language['active']) { $output .= '' . strtoupper($lang_code) . ''; } else { $output .= '' . strtoupper($lang_code) . ''; } // Append separator except for the last language if ($count < $total_languages - 1) { $output .= ' / '; } $count++; } $output .= '

'; return $output; } } return ''; // Return empty string if WPML is not active } add_shortcode('custom_language_switcher', 'custom_language_switcher_shortcode');

Symptoms:
The custom language switcher works on some pages, but on most pages, the second language goes to a random post in this language instead of the current page in the clicked language.

Questions:
Why does the language switcher redirect to a random post instead of the current page in the selected language?
How can I ensure the language switcher works consistently across all pages?

December 19, 2024 at 2:38 pm #16530584

pimv-12

I still need help

December 20, 2024 at 7:14 am #16532218

Prosenjit Barman
Supporter

Languages: English (English )

Timezone: Asia/Dhaka (GMT+06:00)

Hi There!
Thanks for reaching out to WPML Support.

Before this thread gets assigned to my colleague, I'd like to offer some initial guidance or potential solutions.

I’ve reviewed the code and noticed that you’re using the "wpml_permalinks" hook. In cases where links are not directed correctly, we always recommend resetting the permalink structure. You can do this by navigating to "Settings > Permalinks" and simply saving the settings again.

Could you please try this and let us know if it resolves the issue?

If the issue still persists, The problem could lie in the way the translated URL is generated for other languages. In your original code, you're using the `apply_filters('wpml_permalink', $current_url, $lang_code)` function to get the translated URL for the current page. However, this function does not always return the correct translated URL, especially if the current page is not translated yet.

So, before generating the URL, ensure the current page or post ID is passed through the `wpml_object_id` filter. This will retrieve the corresponding translated page or post ID. Once the translated ID is obtained, use the `wpml_permalink` hook to generate the correct translated link. If no translation is available, fall back to the main URL.

Could you please try to modify the code to use the current page's URL when the current language is active?

As providing support for the custom code is beyond our support scope, we can only help by providing guidance or suggestions that might help. For direct coding assistance, consider hiring a developer, experienced in customizing WPML from here: https://wpml.org/contractors/

I hope you can understand. If you have any other questions or need clarification, feel free to reach us. We'll be happy to help.

Best regards,
Prosenjit