WPML
PHP
wpml_browser_redirect_language_params

Description

This filter can be used when the theme developer wants to use a different cookie name or tweak the languageUrls array to use different URLs.

It uses the $params argument which is an associative array containing some information about the current language, the active languages and the cookie settings.

Arguments

  • pageLanguage

    Stores the current language code.
  • languageUrls

    Stores an associative array containing all the current page’s translation URLs. The keys of the array are the language codes. The same language can have multiple entries to match a different combination of language codes. For instance, in the case of en_US, the same URL is repeated three times with the following keys: en_us, en, and us.
  • cookie

    Stores an associative array with the cookie settings. Please refer to Document.cookie (opens in a new tab) for more details. The keys are:
    • name

      the name of the cookie (defaults to _icl_visitor_lang_js).
    • domain

      the domain in which the cookie must be used (defaults to the current domain and is not meant to be changed).
    • path

      the path of the cookie (defaults to /).
    • expiration

      the number of hours the cookie lasts. From WPML 5.3, it is the number set in the Visitor language section of WPMLSettingsURLs and SEO (Redirect each visitor at most once every … hours or After a visitor closes the bar, hide it for … hours). The minimum is 24. In WPML 5.2 and older, it is the number in Remember visitors’ language preference, in the Browser language redirect section of WPMLSettingsLanguages.

From WPML 5.3, the array also has these keys:

  • firstVisitMode

    redirect when the automatic redirect is on, bar when the language bar is on, and keep when neither is.
  • allLanguages

    An ordered list of { code, url, missing } entries, one per language in the language switcher. url is this page in that language. When the translation is missing, url is the home page of that language and missing is true.
  • siteOrigins

    The origins (scheme://host[:port]) of every allLanguages URL and of the home page of every active language. A visitor who comes from one of them is already on the site.
  • isSearch

    Whether the current page is a search results page.
  • chosenLanguage

    An associative array about the language a visitor picks in a language switcher: enabled (whether WPML keeps that language and shows it on the next visit, see wpml_redirect_to_chosen_language), languages (the codes of all active languages) and cookie (the name, domain, path and maxAge in seconds of the cookie that keeps the picked language).
  • bar

    Only when firstVisitMode is bar: the texts of the language bar in each active language (texts), the text direction of each language (dir) and its language tag (tags).

Example Usage

From WPML 5.3, WPML runs this filter when at least one option in the Visitor language section of WPMLSettingsURLs and SEO is on: showing returning visitors the language they picked, the language bar or the automatic redirect. In WPML 5.2 and older, it runs only when Browser language redirect is on in WPMLSettingsLanguages.

Following code can be used to redirect browser to homepage for a post when language is changed to specific one

PHP
function example_callback( $params ) {
	global $post;
	// Redirect the French language to the home page if the current page ID is 123
	if ( $post->ID === 123 ) {
		$params['pageLanguage'] = 'en';
		$params['languageUrls'] = [ 'fr' => 'http://example.com/fr/' ];
	}
	return $params;
}
add_filter( 'wpml_browser_redirect_language_params', 'example_callback' );