Skip to content Skip to sidebar

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 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 -
- - - - - - -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 1 replies, has 2 voices.

Last updated by Waqas Bin Hasan 1 year, 4 months ago.

Assisted by: Waqas Bin Hasan.

Author Posts
May 30, 2024 at 11:33 am #15687632

Andre Van Kets

Background of the issue:
I am trying to manage language visibility on my website using WPML. While I have successfully hidden certain languages from the front-end, I need to ensure that I can still access and manipulate data associated with these hidden languages within the template and plugin code.

the previous support ticker I raised was (frustratingly!) re-written by your support bot. And that probably resulted in your support agent not understanding the important part of my question, which is:

Can we access a hidden language via this code:

$sitepress->switch_lang($lang, true).

Full code sample below:

switch_lang($lang, true);
$drivethru = new drivethru();
$dropdown_final = $drivethru->get_branch_dropdown_values();
foreach($dropdown_final as $parent => $branches) {
if ($parent != 'no_location') {
echo '';
foreach ($branches as $branch_id => $branch_name) {
if ($branch_id != 17191 || $branch_id != 17215 || $branch_id != 17167)
echo '' . $branch_name . '';
}
echo '';
} elseif ($parent == 'no_location') {
echo '';
foreach ($branches as $branch_id => $branch_name) {
echo '' . $branch_name . '';
}
echo '';
}
}
$sitepress->switch_lang($current_lang, true);
?>

Symptoms:
I am unsure how to access posts of a hidden language in the template or plugin code using the WPML function $sitepress->switch_lang($lang, true).

Questions:
Can I access posts of a hidden language in template or plugin code using $sitepress->switch_lang($lang, true)?
Is there any specific documentation or examples that detail accessing posts of a hidden language in code?

May 30, 2024 at 1:07 pm #15688222

Waqas Bin Hasan
WPML Supporter since 05/2014

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for waiting on this.

I consulted with my team and despite the authenticity of your code, you should be able to query the posts from hidden languages. When you hide a language in WPML, it is hidden for visitors and search engines, while you and translators can work on that. Please see https://wpml.org/documentation/getting-started-guide/language-setup/hiding-languages-on-the-front-end/ for details.

All you need to enable the display for yourself in your user profile.

As long as the code is concerned, I'm unable to verify that if it is the correct code (at this point). But as I can understand and mentioned in the chat, that, instead of referencing to internal methods and classes directly, you should use WPML hooks. A most relevant hook to switch a language is https://wpml.org/wpml-hook/wpml_switch_language/. This action hook allows controlling the language in WordPress queries. It overrides the WPML language global variable used to filter database query requests. However, please consider the note mentioned in the hook's guide.

To confirm the authenticity of your proposed code, I have setup this fresh sandbox site (hidden link - one click login) for you. Can you please add your code the way you want and show me an example? So I can further look into this and can have more information from the team. I've configured WPML with few languages. You 'll need to add one or 2 posts, translate these, hide language(s) as you want, add your code and check.

The topic ‘[Closed] Hide languages on the front-end, but still able to access the’ is closed to new replies.