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.

Tagged: 

This topic contains 17 replies, has 2 voices.

Last updated by Santiago 1 year, 9 months ago.

Assisted by: Dražen.

Author Posts
May 2, 2023 at 1:45 am #13566641

Santiago

Hello there,
I like to add a custom link to the language switcher and open it in a new window with the next code but looks like the "url_target" is not working.
Do we have some parameter alternative to open the link on a new window?
Thank you in advance.-


function add_custom_link_to_language_switcher( $languages ) {

    $languages['external'] = array(

        'country_flag_url' => '<em><u>hidden link</u></em>',
        'native_name' => 'External link',
        'url' => '<em><u>hidden link</u></em>',
        'url_target' => '_blank',
        'code' => 'en'

    );

    return $languages;
}

add_filter( 'icl_ls_languages', 'add_custom_link_to_language_switcher' );

May 2, 2023 at 6:52 am #13567187

Dražen
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+01:00)

Hello,

please check the next examples which seem to work for other clients.

- https://wpml.org/documentation/getting-started-guide/language-setup/language-switcher-options/
- https://wpml.org/forums/topic/adding-a-custom-link-to-custom-language-switcher-elementor/

FYI, please note we do not provide custom code service nor custom code support, but we will be glad to help you in the right direction.

Let us know how it goes.

Regards,
Drazen

May 2, 2023 at 7:04 am #13567259

Santiago

Hello and thank you for your update.

I have checked your first link and it's for something different.
And I have checked your second link but is not working.

I need your help to understand what WPML support.
My code should work because there are the right parameters but is not.
Usually with WPML other codes uses $languages[]
And I think $languages['external'] is also a valid one.
Not sure why the external type does not open the links on a new window/tab.
No one what to open an external link on the same window and lost the current page window.

There on https://wpml.org/forums/topic/i-want-to-add-custom-link-to-the-language-switcher/ have a link to https://wpml.org/forums/topic/adding-an-external-link-to-language-switcher/ but that link doesn't exist anymore.

Can you help to make my external link open on a new window/tab.
I can't find any correct solution on the forum.

Thank you in advance.-

May 3, 2023 at 9:22 am #13576651

Dražen
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+01:00)

Hello,

thanks for getting back.

I understand your issue now, sorry for the confusion.

I have checked and do not see any parameter that can be used, but have shared and escalated this to 2nd tier to advise further. Your doubts make sense, and if there is no one I will check for a feature request.

I will update you soon on this, when there is some news.

Regards,
Drazen

May 3, 2023 at 12:50 pm #13578751

Dražen
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+01:00)

Hello,

our 2nd tier checked and seems that the hook and even our code don't allows the "url_target" attribute.

The only way to achieve this is by creating a custom language switcher template and there adjust HTML to open a link in external HTML.

For example:

$dropdown_items .='<li class="menu-item wpml-ls-item wpml-ls-menu-item"><a class="elementor-sub-item" href="' . $custom_language_link . '" target="_blank"><img src="' . $custom_flag_url . '" height="12" alt="" width="18" /> <span class="wpml-ls-native"> ' . $custom_language_name . '</span></a></li>';

Related ticket:
- https://wpml.org/forums/topic/i-want-to-add-custom-link-to-the-language-switcher/

I tested and it works fine.

Please note it is an example code and would need further adjustment if any issues.

Kind regards,
Drazen

May 4, 2023 at 6:25 am #13582999

Santiago

Hello there, and thank you for your update.

Look like the external links do not support any attribute to open a link on a new browser window or tab.
This is pretty sad, everyone needs to open external links on a new browser window or tab.
No one wants to lose their website's pages when open an external link.
ANY CHANGE TO ADD THIS REQUEST TO FUTURE UPDATES?

About https://wpml.org/forums/topic/i-want-to-add-custom-link-to-the-language-switcher/
That code it's for a custom menu to show on the header menu.
I need to add an external link on the language switcher:
do_action('wpml_add_language_selector')

Can you help me with the code to replace the:
do_action('wpml_add_language_selector')

It's a bit disappointing that we can't use something as basic as the target attribute in the simple code I have shared.
And I have tried with many different options:

'url_target' => '_blank',
'target' => '_blank',
'url_options' => array( 'target' => '_blank', 'rel' => 'noopener' ),
'anchor_attributes' => 'target="_blank"',
'url_add_args' => array( 'target' => '_blank' ),

Thank you in advance.-

May 4, 2023 at 6:37 am #13583033

Dražen
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+01:00)

Hello,

Thanks for getting back.

As I understood from our 2nd tier, it is not only a limitation by WPML, but WP as it doesn't allow the "url_target" attribute.

That code shows a language switcher with a custom external link with a custom language link, which you can then adjust to open in a new window, menu and etc. You can further adjust it to your needs and it should work for what you need.

That is 1 way or only other way is to create a custom language switcher via twig templates and adjust it per your needs:

- https://wpml.org/wpml-hook/wpml_language_switcher/
- https://wpml.org/documentation/support/wpml-coding-api/shortcodes/#wpml_language_switcher

I am afraid that is the best we can suggest from our side, and both of the approaches should work fine and allow you to achieve what you are asking.

I think shared documentation and examples should be helpful, to guide you in the correct direction.

Hope it helps and let me know if any doubts. 🙂

Regards,
Drazen

May 4, 2023 at 6:57 am #13583117

Santiago

Hello,

I still trying but the code you have shared shows individual menu items with the different languages.

I need to use a "dropdown click" menu with my languages and the external link.

I have setup a "Custom language switchers" and show with:
do_action('wpml_add_language_selector');

To use the style I have setup for the "Custom language switchers".
The do_action('wpml_add_language_selector'); action outputs the language switcher based on the options set in the Custom Language Switchers section in WPML → Languages.

I don't think the target="_blank" is not a WP limitation.
And also can be used on any header menu item.

Can be a lot easier if the "Custom Language Switchers section in WPML → Languages" allows users to add external links from the UI. Many people would appreciate this.

The code add_filter uses wp_nav_menu_items
That's not correct for a "dropdown click" style right?

I still need your help.
Can you help me with a code to replace the:
do_action('wpml_add_language_selector')

Thank you again.-

May 4, 2023 at 7:55 am #13583505

Dražen
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+01:00)

Hello,

let me check with 2nd tier if we can provide some example code for your case, and I will get back to you soon.

Regards,
Drazen

May 4, 2023 at 8:05 am #13583543

Santiago

Hello,
Basically, I'm using:

function my_language_switcher($block_options){
    do_action('wpml_add_language_selector');
}

I can replace the:
do_action('wpml_add_language_selector');

For the full code where I can show my languages + the external link.
Can be great if you can share a code for that.
And using the do_action('wpml_add_language_selector')
I can still use the style I have setup on the "Custom Language Switchers section in WPML → Languages".

Thank you again.-

May 4, 2023 at 8:11 am #13583559

Dražen
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+01:00)

Hello,

I am afraid I do not think that would be possible, as I have said before, the only way is to create a custom template via TWIG and set it up there correctly as explained in our docs, and then via hook or shortcode show that template:

- https://wpml.org/wpml-hook/wpml_language_switcher/
- https://wpml.org/documentation/support/wpml-coding-api/shortcodes/#wpml_language_switcher

We also do not provide custom coding service from our side, but our 2nd tier is checking your request and we might provide a guide and example code on how to achieve this.

I will update you soon.

Regards,
Drazen

May 4, 2023 at 9:54 am #13584747

Dražen
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+01:00)

Hello,

I am sharing a reply from your 2nd tier:

You can use the next hook for this: https://wpml.org/wpml-hook/wpml_language_switcher/
It should accept template parameters. The template is a TWIG template.

You can get one and customize it from \sitepress-multilingual-cms\templates\language-switchers\ folder. Minimum TWIG template knowledge is required.

Furthermore, the template path could also be registered with the next hook: wpml_ls_template_paths

This can be done in this way:

add_filter ('wpml_ls_template_paths', 'template_inc');

function template_inc(){
	return array('C:\laragon\www\test\wp-content\themes\astra\language-switcher','C:\laragon\www\test\wp-content\themes\astra\language-switcher\testing');
}

Make sure that the path exists there.

- There is also a simpler way of using JavaScript code - which is the easiest solution for me but it requires a known CSS class which is not always set.

Check this out this example relying on JQuery: hidden link"target"%2C "_blank")%3B })%3B-,Example,-Here%20is%20a

Load JQuery in the head:

-

 <script src="<em><u>hidden link</u></em>"></script>

For example for the default language switcher in the footer, we can target its normal class "wpml-ls-link" and add the attribute by this code.

Add this in the body:

<script>
$(document).ready(function(){
   $("a.wpml-ls-link").prop("target", "_blank");
});
</script>

Hope that this helps guide you with an example and in the correct direction.

Regards,
Drazen

May 5, 2023 at 6:46 am #13590985

Santiago

Hello and thank you for your update.

First option, that's a completely different solution.
I'm using a very different way to show my language switcher on the header.
Also, I'm using your recommended option, which is:
do_action('wpml_add_language_selector')
I can't change all my code to switch to your TWIG template option.
Also, I can't use a C:\ path and have problems in my different environments.
This doesn't look like a very practical solution.

Second option, do my original code support a class parameter?
I can't apply the open target _blank on all my language switcher items.
Only need that for the external link.

Can I ask where the code for:
do_action('wpml_add_language_selector')
is located?

Thank you.-

May 5, 2023 at 8:00 am #13591455

Dražen
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+01:00)

Hello,

I am afraid that method is suggested for such an approach when you need a custom language switcher, there is no solution out of the box.

As for the other method:

2) You can just add a class to the external links with code. Here is proof of the concept code:

<!DOCTYPE html>
<html>
<head>
   <script src="<em><u>hidden link</u></em>"></script>
</head>
   <body>
      <a href="<em><u>hidden link</u></em>">Google</a>
      <a href="<em><u>hidden link</u></em>">Facebook</a>
      <a href="<em><u>hidden link</u></em>">Twitter</a>
      <a href="<em><u>hidden link</u></em>">test on localhost</a>
      <script>
         function link_is_external(link_element) {
            return (link_element.host !== window.location.host);
         }

         $(document).ready(function () {
            $('a').each(function () {
               if (link_is_external(this)) {
                  $(this).addClass('external');
               }
            });
            $("a.external").prop("target", "_blank");
         });
      </script>
   </body>
</html>

This adds "external" class. If the link matches the current domain, no class will be added. The process for adding a target is executed on the links with the class "external"

3) The code for wpml_add_language_selector is starting in sitepress-multilingual-cms\classes\language-switcher\public-api\class-wpml-ls-actions.php

For all of the suggested methods you need to develop and code your own solution as advised and suggested by our side, there is no solution out of the box. I am afraid we can not do the coding for you, as per our policy.

Maybe hiring another contractor that could help out further.

Hope you understand and this helps out and guides you correctly.

Regards,
Drazen

May 7, 2023 at 12:49 am #13599565

Santiago

Hello and thank you for your update.
I think I've got a solution for links in new windows.

And I'm facing problems on my WooCoomerce Shop page only with the next code.
All pages work well except the WooCoomerce Shop page.

function custom_language_switcher_links( $languages ) {

    $languages[] = array(
        'url' => '<em><u>hidden link</u></em>',
        'native_name' => 'International site',
        'translated_name' => 'International site',
        'country_flag_url' => 'https://cdn.wpml.org/wp-content/uploads/flags/flag-global.svg',
        'language_code' => 'en',
        'code' => 'en'
    );
    return $languages;
}
add_filter( 'icl_ls_languages', 'custom_language_switcher_links' );

Can you help me to fix it?

Debug.log errors:

[07-May-2023 00:42:25 UTC] PHP Notice: Undefined index: code in /wp-content/plugins/sitepress-multilingual-cms/classes/seo/class-wpml-seo-headlangs.php on line 53
[07-May-2023 00:42:25 UTC] PHP Notice: Undefined index: code in /wp-content/plugins/sitepress-multilingual-cms/classes/url-handling/class-wpml-endpoints-support.php on line 245
[07-May-2023 00:42:25 UTC] PHP Notice: Undefined index: code in /wp-content/plugins/sitepress-multilingual-cms/classes/url-handling/class-wpml-endpoints-support.php on line 247
[07-May-2023 00:42:25 UTC] PHP Notice: Undefined index: country_flag_url in /wp-content/plugins/sitepress-multilingual-cms/classes/language-switcher/class-wpml-ls-model-build.php on line 188
[07-May-2023 00:42:25 UTC] PHP Notice: Undefined index: native_name in /wp-content/plugins/sitepress-multilingual-cms/classes/language-switcher/class-wpml-ls-model-build.php on line 189
[07-May-2023 00:42:25 UTC] PHP Notice: Undefined index: native_name in /wp-content/plugins/sitepress-multilingual-cms/classes/language-switcher/class-wpml-ls-model-build.php on line 194