> 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/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 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).

```markup
<!-- 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).

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

// Optional: keep 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="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:

```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 );
            $navigator_language = str_replace( 'tw', 'zh-HK', $navigator_language );
        }
    }
    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).
