Skip Navigation

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

Problem:
The client was trying to add a custom link in the WPML language switcher by following this thread: https://wpml.org/forums/topic/adding-an-external-link-to-language-switcher/ but code was throwing errors.

Solution:
I have fixed this issue by updating language switcher code as follows:

add_filter('wp_nav_menu_items', 'new_nav_menu_items', 10, 2);
function new_nav_menu_items($items, $args) {
    
    // get languages
    $languages = apply_filters( 'wpml_active_languages', NULL, 'skip_missing=0' );
 
    // add $args->theme_location == 'primary-menu' in the conditional if we want to specify the menu location.
 	$dropdown_items = '';
    if ( $languages) {
 
		foreach($languages as $language){
			if(!$language['active']){
				
				$dropdown_items .= '<li class="menu-item wpml-ls-item wpml-ls-menu-item"><a class="elementor-sub-item" href="' . $language['url'] . '"><img src="' . $language['country_flag_url'] . '" height="12" alt="' . $language['language_code'] . '" width="18" /> <span class="wpml-ls-native" lang="' . $language['language_code'] . '"> ' . $language['native_name'] . '</span></a></li>';
			}
			else {
				$active_language = '<li class="menu-item wpml-ls-item wpml-ls-current-language wpml-ls-menu-item wpml-ls-first-item menu-item-type-wpml_ls_menu_item menu-item-object-wpml_ls_menu_item menu-item-has-children"><a title="' . $language['native_name'] . '" href="' . $language['url'] . '" class="elementor-item has-submenu"><img class="wpml-ls-flag" src="' . $language['country_flag_url'] . '" alt=""> <span class="wpml-ls-native" lang="' . $language['language_code'] . '"> ' . $language['native_name'] . '</span></a>';
					
			}
		}
		
		/* Start - Custom Link */
		// Please update flag, name and link
		$custom_flag_url = "siteurl/wp-content/plugins/sitepress-multilingual-cms/res/flags/en.png";
		$custom_language_name = "Custom Language";
		$custom_language_link = "https://google.com";
		
		$dropdown_items .='<li class="menu-item wpml-ls-item wpml-ls-menu-item"><a class="elementor-sub-item" href="' . $custom_language_link . '"><img src="' . $custom_flag_url . '" height="12" alt="" width="18" /> <span class="wpml-ls-native"> ' . $custom_language_name . '</span></a></li>';
		/* End - Custom Link */
		
		$final_switcher = $active_language. '<ul class="sub-menu elementor-nav-menu--dropdown sm-nowrap">'.$dropdown_items.'</ul></li>';
    }
 
    return $items.$final_switcher;
}

100% of people find this useful.

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 topic contains 2 replies, has 2 voices.

Last updated by kasperA 2 years, 12 months ago.

Assisted by: Noman.

Author Posts
December 8, 2021 at 9:18 am

kasperA

I'd like to do add a new language to WPML, but one that isn't maintained on my site. I'd like to have a language in the switcher that simply links to another domain - like here, but I can't get it to work: https://wpml.org/forums/topic/adding-an-external-link-to-language-switcher/

December 8, 2021 at 10:23 am
December 8, 2021 at 12:08 pm #10146853

Noman
Supporter

Languages: English (English )

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

I have updated the language switcher code in your child theme functions.php file, now you just need to update the custom language flag, link and name at line 54.

I hope it will resolve your issue.

Thank you

custom lang 1.jpg