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


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://developers.weglot.com/cms/wordpress/use-cases/use-custom-language-code-url-lang-and-hreflang-attribute.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
