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.

This thread is resolved. Here is a description of the problem and solution.

Problem: Custom langauge switcher not working for logged out users.

Solution: The code was fine but the language was hidden in WPML -> Languages.

Relevant Documentation:

This topic contains 2 replies, has 2 voices.

Last updated by brunoD-31 12 months ago.

Assigned support staff: Ricardo Alday.

Author Posts
October 16, 2018 at 1:32 pm #2821124

brunoD-31

I am trying to:
Create a custom WPML language switcher in the header of my page

Link to a page where the issue can be seen:
Coming soon, restaging the website

I expected to see:
A language switcher in the top right corner as seen here:
hidden link

Instead, I got:
It's blank if a user is not logged in, or not admin (i'm not sure):
hidden link

Here's the code I used:

The header-logo.php file:
https://pastebin.com/CmS8HCGB
This snippet:
<div class="desktop">
<div id="flags_language_selector"><?php language_selector_flags_opposite(); ?></div >
</div>

The functions.php file:
https://pastebin.com/uzGcVVfx
This snippet:
/* Flags but in opposite order */
function language_selector_flags_opposite() {
$languages = icl_get_languages('skip_missing=0');
if(!empty($languages)){
foreach($languages as $l){
if(!$l['active']){
echo '<a href="' . $l['url'] . '"> ' . $l['translated_name'] . ' <img class="country-flag" src="' . $l['country_flag_url'] . '" height="12" alt="' . $l['language_code'] . '" width="18" /></a>';
}
}
}
}

Why is this not viewable by guests?

October 17, 2018 at 5:46 pm #2825565

Ricardo Alday

Hi,

Thank you for contacting WPML support.
I tested your code in my local installation and it works, whether I'm logged in or out.

Are the languages hidden by any change on your site?

October 18, 2018 at 10:22 am #2827583

brunoD-31

The language was indeed still hidden.

I was developing this function on a staging environment where the language was still hidden.

Issue solved.