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

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

By default, Weglot provides a **server-side auto-switch** feature. This works well in most cases, but sometimes it can be affected by caching systems or specific plugins that modify the request or response cycle. When that happens, the redirection may not behave as expected.\
\
To overcome these limitations, we’ve implemented a **client-side auto-switch** using Weglot’s JavaScript library.

1. **Disable the server-side auto-switch**\
   In your WordPress admin panel, go to **Weglot → Settings** and turn off the *Auto-switch* option.
2. **Add the following filters to your theme or custom plugin**\
   \
   Add and activate Code Snippets plugin on your WordPress: <https://wordpress.org/plugins/code-snippets/>\
   \
   In your WordPress back office, go to Snippets -> Add new\
   Add a title (example: `Weglot - Autoswitch js`)<br>

   1. Add the following code into the code input.
   2. Save changes<br>

   ```php
   function custom_weglot_dynamics_selectors( $default_dynamics ) {
       return [];
   }
   add_filter( 'weglot_dynamics_selectors', 'custom_weglot_dynamics_selectors' );
   add_filter( 'weglot_whitelist_selectors', 'custom_weglot_dynamics_selectors' );
   add_filter( 'weglot_translate_dynamics', '__return_true' );
   add_filter( 'weglot_allowed_urls', function( $urls ) {
       return 'all';
   });
   ```

   \
   These filters ensure that Weglot’s JS library is properly loaded and ready to handle client-side switching.
3. Add the following filter:<br>

   ```php
   add_filter( 'weglot_autoredirect_js', '__return_true' );
   ```

   \
   Once this filter is active, you can trigger the auto-switch logic manually if needed:<br>

   ```php
   $js_autoswitch = apply_filters( 'weglot_autoredirect_js', false );
   ```

You can Also add the filter using mu-plugin or in the functions.php file in your theme\
\
With this setup, Weglot will perform the language auto-switch **client-side via JavaScript**, ensuring compatibility with caching systems and other plugins that may 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://developers.weglot.com/wordpress/autoswitch-system-server-side-and-client-side-implementation.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
