For the complete documentation index, see llms.txt. This page is also available as Markdown.

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 plugin, then go to Snippets → Add New, give it a title (e.g. Weglot - Autoswitch JS), paste the code below, and save:

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.

Last updated