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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Europe/Tirane (GMT+02:00)

This topic contains 3 replies, has 2 voices.

Last updated by Marsel Nikolli 1 year, 2 months ago.

Assisted by: Marsel Nikolli.

Author Posts
April 22, 2024 at 6:56 pm #15552812

frankK-26

function custom_language_switcher() {

$languages = icl_get_languages('skip_missing=0&link_empty_to=str');
echo '<div class="custom-language-switcher">';

echo '<svg xmlns="hidden link" width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="#ffffff" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><path d="M12 2a14.5 14.5 0 0 0 0 20 14.5 14.5 0 0 0 0-20"/><path d="M2 12h20"/></svg>';

echo '<svg xmlns="hidden link" width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="#ffffff" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round" class="chevron-icon"><path d="m6 9 6 6 6-6"/></svg>';

echo '<div class="language-dropdown">';

foreach($languages as $l) {

if(!$l['missing']) {

echo '' . $l['native_name'] . '';

}

}

echo '</div></div>';

}

add_shortcode('custom_language_switcher', 'custom_language_switcher');

it still redirects me to home page on some instances - usually, works well, but feels buggy. Let's say im on About Us page, sometimes it redirects me to home page, sometimes changes the language of current page perfectly

When I navigate between pages and hover the lang switcher a element, sometimes it shows the correct URL it should direct me on lang change, sometimes its just landing page / home page.

April 23, 2024 at 10:05 am #15554922

Marsel Nikolli

Hi,

Thank you for contacting WPML Support, I will gladly help you.

Please note that custom coding is not covered by our Support Policy. The code provided might not work on your website, and in case you feel that you need more help with this, I recommend that you hire a developer or a WPML contractor: https://wpml.org/contractors.

Here is an updated version of your function with added debugging to help track down the issue:

function custom_language_switcher() {
    $languages = icl_get_languages('skip_missing=0&link_empty_to=str');
    $current_url = (is_ssl() ? '<em><u>hidden link</u></em>' : '<em><u>hidden link</u></em>') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

    echo '<div class="custom-language-switcher">';
    echo '<svg xmlns="<em><u>hidden link</u></em>" width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="#ffffff" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><path d="M12 2a14.5 14.5 0 0 0 0 20 14.5 14.5 0 0 0 0-20"/><path d="M2 12h20"/></svg>';
    echo '<svg xmlns="<em><u>hidden link</u></em>" width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="#ffffff" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round" class="chevron-icon"><path d="m6 9 6 6 6-6"/></svg>';
    echo '<div class="language-dropdown">';

    foreach ($languages as $l) {
        if (!$l['missing']) {
            // Generate the correct URL for the current page in the new language
            $lang_url = add_query_arg('lang', $l['language_code'], $current_url);
            echo '<a class="td-text" href="' . $lang_url . '">' . $l['native_name'] . '</a>';
        }
    }

    echo '</div></div>';
}

add_shortcode('custom_language_switcher', 'custom_language_switcher');

1) Current URL Calculation: It dynamically calculates the current URL and appends the appropriate language parameter. This should help in maintaining the context of the current page while switching languages.

2) Debug Information: You might want to log or examine $current_url and the final URLs ($lang_url) to see if they're being constructed as expected.

* Given that the issue is somehow random I would suggest checking the cache of the website as well.

I suggest you test the code provided in a staging environment to avoid any possible issues that might be caused by it.

I hope this information is helpful and thanks for understanding.

Kind regards,
Marsel

April 30, 2024 at 3:46 pm #15582072

frankK-26

Hi,

Thanks for the reply.
Unfortunately this did not solve for the issue.
It only added '?lang=x' query to URL, which.. is not right for the setup.

URL structure is domain.xx/en or domain.xx/otherlang/. So it uses language folders.

The website indeed has caching implemented, but... this seems more like the issue of WPML not custom coding - if it works, but inconsistently, i feel like issue must be somewhere else...

Thanks!

April 30, 2024 at 4:30 pm #15582200

Marsel Nikolli

Hi,

Please try this other approach, which might better fit for your setup:

function custom_language_switcher() {
    $languages = icl_get_languages('skip_missing=0&link_empty_to=str');
    $current_url = (is_ssl() ? '<em><u>hidden link</u></em>' : '<em><u>hidden link</u></em>') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
 
    echo '<div class="custom-language-switcher">';
    echo '<svg xmlns="<em><u>hidden link</u></em>" width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="#ffffff" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><path d="M12 2a14.5 14.5 0 0 0 0 20 14.5 14.5 0 0 0 0-20"/><path d="M2 12h20"/></svg>';
    echo '<svg xmlns="<em><u>hidden link</u></em>" width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="#ffffff" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round" class="chevron-icon"><path d="m6 9 6 6 6-6"/></svg>';
    echo '<div class="language-dropdown">';
 
    foreach ($languages as $l) {
        if (!$l['missing']) {
            // Generate the correct URL for the current page in the new language
            $lang_url = home_url($l['language_code'] . $_SERVER['REQUEST_URI']);
            echo '<a class="td-text" href="' . $lang_url . '">' . $l['native_name'] . '</a>';
        }
    }
 
    echo '</div></div>';
}
 
add_shortcode('custom_language_switcher', 'custom_language_switcher');

Please note, as I previously mentioned, custom coding is not covered by our Support Policy. The code provided might not work on your website.

Kind regards,
Marsel

The topic ‘[Closed] custom lang switcher not working consistently’ is closed to new replies.