Use custom language code (URL, lang and hreflang attribute)
Here we explain how to change the default language code. This is useful when you don't like Weglot's default 2-letter code in the URL and want to replace it with another one.
Example: change "tw" to "zh-HK"
Below we change the language code tw (Traditional Chinese) into zh-HK (Hong Kong Chinese).
<!-- HTML output by default (Traditional Chinese) -->
<html lang="tw">
<head>
<link rel="alternate" hreflang="en" href="https://mysite.com/"/>
<link rel="alternate" hreflang="tw" href="https://mysite.com/tw/"/>
</head>
</html>
<!-- HTML output with custom language code (Hong Kong Chinese) -->
<html lang="zh-HK">
<head>
<link rel="alternate" hreflang="en" href="https://mysite.com/"/>
<link rel="alternate" hreflang="zh-HK" href="https://mysite.com/zh-HK/"/>
</head>
</html>Hook to customize the language code
This hook must be loaded before your theme, so it cannot live in your functions.php. Use one of the two methods below (Code Snippets plugin or an MU-plugin).
With the auto-redirect option
If the auto-redirect option is enabled, also map your custom code in the browser-language list with the weglot_navigator_language filter:
How to load it
Method 1 — Code Snippets plugin: install Code Snippets, go to Snippets → Add new, paste the code (without the <?php tag, the plugin adds it) and save.
Method 2 — MU-plugin: create /wp-content/mu-plugins/weglot-language-code-replace.php and paste the code (with the <?php tag).
Last updated