> 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/autoswitch-system-server-side-and-client-side-implementation.md).

# Autoswitch System: Server-Side and Client-Side implementation

Weglot uses an auto-switch mechanism to automatically redirect users to the correct language version of the site based on their browser settings .

By default, Weglot provides a **server-side auto-switch** that redirects visitors to the language version matching their browser settings. This works well in most cases, but caching systems or plugins that alter the request/response cycle can prevent the redirect from firing as expected.

To work around those limitations, Weglot can also run the auto-switch **client-side**, using its JavaScript library.

#### 1. Disable the server-side auto-switch

In your WordPress admin, go to **Weglot → Settings** and turn off the **Auto-switch** option.

#### 2. Enable the client-side auto-switch

Install and activate the [Code Snippets](https://wordpress.org/plugins/code-snippets/) plugin, then go to **Snippets → Add New**, give it a title (e.g. `Weglot - Autoswitch JS`), paste the code below, and save:

```php
function custom_weglot_empty_selectors( $selectors ) {
    return [];
}
add_filter( 'weglot_whitelist_selectors', 'custom_weglot_empty_selectors' );
add_filter( 'weglot_dynamics_selectors', 'custom_weglot_empty_selectors' );

add_filter( 'weglot_translate_dynamics', '__return_true' );
add_filter( 'weglot_allowed_urls', function () {
    return 'all';
} );
add_filter( 'weglot_autoredirect_js', '__return_true' );
```

What each filter does:

* **`weglot_translate_dynamics`** — loads Weglot's JavaScript library on the page (required for client-side switching).
* **`weglot_allowed_urls`** — returning `'all'` loads the script on every page; without it the script never loads.
* **`weglot_whitelist_selectors` / `weglot_dynamics_selectors`** — returning an empty array disables dynamic content translation, so the library only performs the auto-switch redirect and doesn't re-translate the page on the fly.
* **`weglot_autoredirect_js`** — the key filter: it turns the client-side auto-switch on.

You can add these filters through Code Snippets, an mu-plugin, or your theme's `functions.php`.

With this setup, Weglot performs the language auto-switch **client-side via JavaScript**, keeping it compatible with caching systems and other plugins that interfere with server-side redirects.


---

# 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/autoswitch-system-server-side-and-client-side-implementation.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.
