get_locale(): string
- Since
- 1.5.0
- Source
wp-includes/l10n.php:30
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
- Scaling
- Constant
- Instructions
- 9–39
- Plugin surface
- 1 hook
- Called by
- 28
Reads stored settings via get_site_option(), cached per request but not free on a cold cache.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 56.
Third-party callbacks on 'locale' run inside this call, and their cost is not bounded by anything here.
28 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - optionnetwork option
get_site_option()called directly - cacheobject cache
wp_cache_get()one call below get_locale() - serializeserialisation
maybe_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.
| When | Instructions | Calls it makes |
|---|---|---|
isset($locale) | 9 | apply_filters() |
!isset($locale) && !is_multisite() | 24–29 | is_multisite(), get_option(), apply_filters() |
!isset($locale) && is_multisite() && wp_installing() | 28–34 | is_multisite(), wp_installing(), get_site_option(), apply_filters() |
!isset($locale) && is_multisite() && !wp_installing() && $ms_locale !== false | 29–35 | is_multisite(), wp_installing(), get_option(), apply_filters() |
!isset($locale) && is_multisite() && !wp_installing() && $ms_locale === false | 33–39 | is_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:
- apply_filters( locale )filterline 35 (+5 into the body)
Filters the locale ID of the WordPress installation.
- apply_filters( locale )filterline 80 (+50 into the body)
Filters the locale ID of the WordPress installation.
Uses · 5
- apply_filters()Calls the callback functions that have been added to a filter hook.
- is_multisite()Determines whether Multisite is enabled.
- wp_installing()Checks or sets whether WordPress is in "installation" mode.
- get_site_option()Retrieve an option value for the current network based on name of option.
- get_option()Retrieves an option value based on an option name.
Used by · 28
- WP_Automatic_Updater::send_debug_email()Prepares and sends an email of a full log of background update results, useful for debugging and geekery.
- WP_Automatic_Updater::send_email()Sends an email upon the completion or failure of a background core update.
- WP_Automatic_Updater::send_plugin_theme_email()Sends an email upon the completion or failure of a plugin or theme background update.
- WP_Debug_Data::get_wp_core()Gets the WordPress core section of the debug data.
- WP_Recovery_Mode_Email_Service::send_recovery_mode_email()Sends the Recovery Mode email to the site admin email address.
- _wp_privacy_send_erasure_fulfillment_notification()Notifies the user when their erasure request is fulfilled.
- determine_locale()Determines the current locale desired for the request.
- get_locale_stylesheet_uri()Retrieves the localized stylesheet URI.
- get_user_locale()Retrieves the locale of a user.
- insert_with_markers()Inserts an array of strings into a file (.htaccess), placing it between BEGIN and END markers.
- list_core_update()Lists available core updates.
- list_translation_updates()Display the update translations form.
Show all 28
- login_header()Outputs the login page header.
- populate_network_meta()Creates WordPress network meta and sets the default values.
- remove_accents()Converts all accent characters to ASCII characters.
- translations_api()Retrieve translations from WordPress Translation API.
- wp_get_image_alttext()Gets the alt text from image meta data.
- wp_maybe_decline_date()Determines if the date should be declined.
- wp_new_user_notification()Emails login credentials to a newly-registered user.
- wp_notify_moderator()Notifies the moderator of the site about a new comment that is awaiting approval.
- wp_notify_postauthor()Notifies an author (and/or others) of a comment/trackback/pingback on a post.
- wp_password_change_notification()Notifies the blog admin of a user changing password, normally via email.
- wp_privacy_send_personal_data_export_email()Send an email to the user with a link to the personal data export file
- wp_register_core_abilities()Registers the default core abilities.
- wp_send_user_request()Send a confirmation request email to confirm an action.
- wp_timezone_choice()Gives a nicely-formatted list of timezone strings.
- wp_version_check()Checks WordPress version against the newest version.
- wpmu_new_site_admin_notification()Notifies the Multisite network administrator that a new site was created.
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.
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.