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: Custom Work
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, 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/ 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, |
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. I need your help to understand what WPML support. 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. 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, |
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: I tested and it works fine. Please note it is an example code and would need further adjustment if any issues. Kind regards, |
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. About https://wpml.org/forums/topic/i-want-to-add-custom-link-to-the-language-switcher/ Can you help me with the code to replace the: It's a bit disappointing that we can't use something as basic as the target attribute in the simple code I have shared. 'url_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/ 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, |
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: To use the style I have setup for the "Custom language switchers". I don't think the target="_blank" is not a WP limitation. 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 I still need your help. 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, |
May 4, 2023 at 8:05 am #13583543 | |
Santiago |
Hello, function my_language_switcher($block_options){ do_action('wpml_add_language_selector'); } I can replace the: For the full code where I can show my languages + the external link. 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/ 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, |
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/ 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, |
May 5, 2023 at 6:46 am #13590985 | |
Santiago |
Hello and thank you for your update. First option, that's a completely different solution. Second option, do my original code support a class parameter? Can I ask where the code for: 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, |
May 7, 2023 at 12:49 am #13599565 | |
Santiago |
Hello and thank you for your update. And I'm facing problems on my WooCoomerce Shop page only with the next code. 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 |