The core_version_check_locale WordPress PHP filter allows you to modify the locale requested for WordPress core translations.
Usage
add_filter('core_version_check_locale', 'your_custom_function_name'); function your_custom_function_name($locale) { // your custom code here return $locale; }
Parameters
$locale
: (string) The current locale.
More information
See WordPress Developer Resources: core_version_check_locale
Examples
Change locale to French
Change the locale to French (fr_FR) for core translations.
add_filter('core_version_check_locale', 'change_locale_to_french'); function change_locale_to_french($locale) { $locale = 'fr_FR'; return $locale; }
Set locale based on user preference
Set the locale based on a user’s preference stored in user meta.
add_filter('core_version_check_locale', 'set_locale_based_on_user_preference'); function set_locale_based_on_user_preference($locale) { $user_id = get_current_user_id(); $user_locale = get_user_meta($user_id, 'preferred_locale', true); if (!empty($user_locale)) { $locale = $user_locale; } return $locale; }
Force locale for a specific user role
Force the locale to Spanish (es_ES) for users with the ‘editor’ role.
add_filter('core_version_check_locale', 'force_locale_for_editors'); function force_locale_for_editors($locale) { if (current_user_can('editor')) { $locale = 'es_ES'; } return $locale; }
Change locale based on browser language
Change the locale based on the user’s browser language.
add_filter('core_version_check_locale', 'set_locale_based_on_browser_language'); function set_locale_based_on_browser_language($locale) { if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { $browser_locale = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); if ($browser_locale == 'de') { $locale = 'de_DE'; } } return $locale; }
Set locale based on a custom query parameter
Set the locale based on a custom query parameter in the URL.
add_filter('core_version_check_locale', 'set_locale_based_on_query_parameter'); function set_locale_based_on_query_parameter($locale) { if (isset($_GET['custom_locale'])) { $custom_locale = sanitize_text_field($_GET['custom_locale']); if (!empty($custom_locale)) { $locale = $custom_locale; } } return $locale; }