How to get locale within WP REST Request?

There’s only 2 real places a locale is defined in a vanilla WordPress install:

  1. For the site.
  2. For the user account.

In the admin the user account’s locale will be used, while on the front end the site’s locale will be used. For REST API requests the user locale can be used by passing _locale=user as a parameter.

The determine_locale() function is what performs the logic to determine whether the user or site’s locale should be used, with additional logic that handles setting the locale during install.

The get_locale() function is used by determine_locale to get the site’s locale. It returns the site’s language as set in Settings > General. The site language can also be defined as a constant in wp-config.php or filtered by plugins. This is the function you’d normally use to get a site’s locale.

Individual pages and posts do not have a locale. If you are using a plugin to create versions of pages in multiple languages then you will need to refer to that plugin’s documentation for the correct way to get the correct language.

If you’re asking how to get the user’s location, then that’s something else entirely, and not something WordPress can do. To accomplish that you will either need to use JavaScript in the browser to ask the user to share their location, or use a database of IP addresses to estimate the user’s location. WordPress does not provide a function for either of these things.

tech