> For the complete documentation index, see [llms.txt](https://developers.weglot.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers.weglot.com/cms/wordpress/use-cases/use-custom-language-code-url-lang-and-hreflang-attribute.md).

# 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 code in the URL and want to replace it with another one.

#### Example: change "zh-tw" to "zh-HK"

Below we change the language code `zh-tw` (Traditional Chinese) into `zh-HK` (Hong Kong Chinese).

```markup
<!-- HTML output by default (Traditional Chinese) -->
<html lang="zh-tw">
    <head>
        <link rel="alternate" hreflang="en" href="https://mysite.com/"/>
        <link rel="alternate" hreflang="zh-tw" href="https://mysite.com/zh-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).

The array key is the Weglot language code (`zh-tw` for Traditional Chinese, as listed in your Weglot dashboard), the value is the code you want to use instead. A key that matches no Weglot language is silently ignored.

```php
// Use zh-HK (Hong Kong Chinese) instead of zh-tw (Traditional Chinese)
add_filter( 'weglot_language_code_replace', 'custom_weglot_language_code_replace' );
function custom_weglot_language_code_replace( $replacements ) {
    $replacements['zh-tw'] = 'zh-HK';
    return $replacements;
}

// Optional: keep zh-tw instead of zh-HK on the hreflang tags
add_filter( 'weglot_href_lang', 'custom_weglot_href_lang' );
function custom_weglot_href_lang( $render ) {
    $render = str_replace( 'hreflang="zh-HK"', 'hreflang="zh-tw"', $render );
    return $render;
}
```

#### 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. Weglot compares the browser languages with your language codes in lowercase, so map `zh-tw` to `zh-hk`:

```php
add_filter( 'weglot_navigator_language', 'custom_weglot_navigator_language' );
function custom_weglot_navigator_language( $navigator_languages ) {
    $navigator_languages = array();
    if ( isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ) { // phpcs:ignore
        $navigator_languages = explode( ',', trim( sanitize_text_field( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ) );
        foreach ( $navigator_languages as &$navigator_language ) {
            if ( strpos( $navigator_language, ';' ) !== false ) {
                $navigator_language = substr( $navigator_language, 0, strpos( $navigator_language, ';' ) );
            }
            $navigator_language = strtolower( $navigator_language );
            if ( 'zh-tw' === $navigator_language ) {
                $navigator_language = 'zh-hk';
            }
        }
    }
    return $navigator_languages;
}
```

#### How to load it

**Method 1 — Code Snippets plugin:** install [Code Snippets](https://wordpress.org/plugins/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).
