pre_determine_locale is a WordPress PHP filter that allows you to override the default locale determination process for the current request.
Usage
add_filter('pre_determine_locale', 'my_custom_locale'); function my_custom_locale($locale) { // your custom code here return $locale; }
Parameters
$locale
(string|null) – The locale you want to return and short-circuit. Default is null.
More information
See WordPress Developer Resources: pre_determine_locale
Examples
Change Locale Based on User Role
Change the locale for logged-in users with the “editor” role.
add_filter('pre_determine_locale', 'change_locale_for_editors'); function change_locale_for_editors($locale) { if (current_user_can('editor')) { $locale = 'en_GB'; } return $locale; }
Change Locale Based on URL Parameter
Set the locale based on the “lang” parameter in the URL.
add_filter('pre_determine_locale', 'change_locale_based_on_url_parameter'); function change_locale_based_on_url_parameter($locale) { if (isset($_GET['lang']) && !empty($_GET['lang'])) { $locale = sanitize_text_field($_GET['lang']); } return $locale; }
Change Locale Based on Cookie
Set the locale based on a cookie value.
add_filter('pre_determine_locale', 'change_locale_based_on_cookie'); function change_locale_based_on_cookie($locale) { if (isset($_COOKIE['language']) && !empty($_COOKIE['language'])) { $locale = sanitize_text_field($_COOKIE['language']); } return $locale; }
Force Locale for Admin Area
Force a specific locale for the admin area.
add_filter('pre_determine_locale', 'force_locale_for_admin_area'); function force_locale_for_admin_area($locale) { if (is_admin()) { $locale = 'en_US'; } return $locale; }
Disable Locale Change
Prevent any changes to the site’s locale.
add_filter('pre_determine_locale', 'disable_locale_change'); function disable_locale_change($locale) { return 'en_US'; }