wppaste
WordPress

get_locale(): string

Since
1.5.0
Source
wp-includes/l10n.php:30
Retrieves the current locale.

Description

If the locale is set, then it will filter the locale in the 'locale' filter hook and return the value.

If the locale is not set already, then the WPLANG constant is used if it is defined. Then it is filtered through the 'locale' filter hook and the value for the locale global set and the locale is returned.

The process to get the locale should only be done once, but the locale will always be filtered using the 'locale' hook.

Compatibility

WordPress
since 1.5.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Return value

string
The locale of the blog or from the 'locale' hook.

Performance profile

How much work a call to get_locale() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Moderate

Reads stored settings via get_site_option(), cached per request but not free on a cold cache.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
9–39

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 56.

Plugin surface
1 hook

Third-party callbacks on 'locale' run inside this call, and their cost is not bounded by anything here.

Called by
28

28 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • optionnetwork optionget_site_option()called directly
  • cacheobject cachewp_cache_get()one call below get_locale()
  • serializeserialisationmaybe_unserialize()one call below get_locale()

Further down the call graph this can also reach query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 5 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_locale() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
isset($locale)9apply_filters()
!isset($locale) && !is_multisite()24–29is_multisite(), get_option(), apply_filters()
!isset($locale) && is_multisite() && wp_installing()28–34is_multisite(), wp_installing(), get_site_option(), apply_filters()
!isset($locale) && is_multisite() && !wp_installing() && $ms_locale !== false29–35is_multisite(), wp_installing(), get_option(), apply_filters()
!isset($locale) && is_multisite() && !wp_installing() && $ms_locale === false33–39is_multisite(), wp_installing(), get_option(), get_site_option(), apply_filters()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 56 instructions, 9–39 executed per call, 9 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while get_locale() runs, in this order:

  1. apply_filters( locale )filterline 35 (+5 into the body)

    Filters the locale ID of the WordPress installation.

  2. apply_filters( locale )filterline 80 (+50 into the body)

    Filters the locale ID of the WordPress installation.

Uses · 5

Used by · 28

Show all 28

Source code

function get_locale() {	global $locale, $wp_local_package; 	if ( isset( $locale ) ) {		/** This filter is documented in wp-includes/l10n.php */		return apply_filters( 'locale', $locale );	} 	if ( isset( $wp_local_package ) ) {		$locale = $wp_local_package;	} 	// WPLANG was defined in wp-config.	if ( defined( 'WPLANG' ) ) {		$locale = WPLANG;	} 	// If multisite, check options.	if ( is_multisite() ) {		// Don't check blog option when installing.		if ( wp_installing() ) {			$ms_locale = get_site_option( 'WPLANG' );		} else {			$ms_locale = get_option( 'WPLANG' );			if ( false === $ms_locale ) {				$ms_locale = get_site_option( 'WPLANG' );			}		} 		if ( false !== $ms_locale ) {			$locale = $ms_locale;		}	} else {		$db_locale = get_option( 'WPLANG' );		if ( false !== $db_locale ) {			$locale = $db_locale;		}	} 	if ( empty( $locale ) ) {		$locale = 'en_US';	} 	/**	 * Filters the locale ID of the WordPress installation.	 *	 * @since 1.5.0	 *	 * @param string $locale The locale ID.	 */	return apply_filters( 'locale', $locale );}

Changelog

Introduced in 1.5.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/l10n.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.