get_language_attributes( string $doctype = 'html' ): string
- Since
- 4.3.0
- Source
wp-includes/general-template.php:4516
Description
Builds up a set of HTML attributes containing the text direction and language information for the page.
Compatibility
- WordPress
- since 4.3.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.
Parameters
$doctypestringoptional- The type of HTML document. Accepts 'xhtml' or 'html'. Default 'html'.Default:
'html'
Return value
string- A space-separated list of language attributes.
Performance profile
How much work a call to get_language_attributes() 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
- 18–50
- Plugin surface
- 1 hook
- Called by
- 4
Reads stored settings via get_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 50.
Third-party callbacks on 'language_attributes' run inside this call, and their cost is not bounded by anything here.
4 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- optionoption read or write
get_option()called directly - hookthird-party callbacks
apply_filters()called directly - cacheobject cache
wp_cache_get()one call below get_language_attributes() - serializeserialisation
maybe_unserialize()one call below get_language_attributes()
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 · 10 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_language_attributes() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!function_exists() | 18 | function_exists(), get_bloginfo(), apply_filters() |
function_exists() | 21–22 | function_exists(), is_rtl(), get_bloginfo(), apply_filters() |
!function_exists() && $doctype !== "html" && $doctype !== "xhtml" | 32 | function_exists(), get_bloginfo(), get_option(), get_option(), apply_filters() |
function_exists() && $doctype !== "html" && $doctype !== "xhtml" | 35–36 | function_exists(), is_rtl(), get_bloginfo(), get_option(), get_option(), apply_filters() |
!function_exists() && $doctype !== "xhtml" | 37–39 | function_exists(), get_bloginfo(), get_option(), esc_attr(), get_option(), apply_filters() |
!function_exists() && $doctype !== "html" | 37–39 | function_exists(), get_bloginfo(), get_option(), get_option(), esc_attr(), apply_filters() |
function_exists() && $doctype !== "xhtml" | 40–43 | function_exists(), is_rtl(), get_bloginfo(), get_option(), esc_attr(), get_option(), apply_filters() |
function_exists() && $doctype !== "html" | 40–43 | function_exists(), is_rtl(), get_bloginfo(), get_option(), get_option(), esc_attr(), apply_filters() |
!function_exists() | 42–46 | function_exists(), get_bloginfo(), get_option(), esc_attr(), get_option(), esc_attr(), apply_filters() |
function_exists() | 45–50 | function_exists(), is_rtl(), get_bloginfo(), get_option(), esc_attr(), get_option(), esc_attr(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 50 | 18–50 | 7 | |
| 8.5 | 50 | 18–50 | 7 | |
| 8.4 | 50 | 18–50 | 7 | 4 fewer instructions than PHP 8.3 |
| 8.3 | 54 | 22–54 | 7 | |
| 8.2 | 54 | 22–54 | 7 | |
| 8.1 | 54 | 22–54 | 7 | |
| 7.4 | 54 | 22–54 | 7 |
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 · 1
One hook fires while get_language_attributes() runs, in this order:
- apply_filters( language_attributes )filterline 4545 (+29 into the body)
Filters the language attributes for display in the 'html' tag.
Uses · 5
- is_rtl()Determines whether the current locale is right-to-left (RTL).
- get_bloginfo()Retrieves information about the current site.
- get_option()Retrieves an option value based on an option name.
- esc_attr()Escaping for HTML attributes.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 4
- WP_Sitemaps_Stylesheet::get_sitemap_index_stylesheet()Returns the escaped XSL for the index sitemaps.
- WP_Sitemaps_Stylesheet::get_sitemap_stylesheet()Returns the escaped XSL for all sitemaps, except index.
- _default_wp_die_handler()Kills WordPress execution and displays HTML page with an error message.
- language_attributes()Displays the language attributes for the 'html' tag.
Source code
function get_language_attributes( $doctype = 'html' ) { $attributes = array(); if ( function_exists( 'is_rtl' ) && is_rtl() ) { $attributes[] = 'dir="rtl"'; } $lang = get_bloginfo( 'language' ); if ( $lang ) { if ( 'text/html' === get_option( 'html_type' ) || 'html' === $doctype ) { $attributes[] = 'lang="' . esc_attr( $lang ) . '"'; } if ( 'text/html' !== get_option( 'html_type' ) || 'xhtml' === $doctype ) { $attributes[] = 'xml:lang="' . esc_attr( $lang ) . '"'; } } $output = implode( ' ', $attributes ); /** * Filters the language attributes for display in the 'html' tag. * * @since 2.5.0 * @since 4.3.0 Added the `$doctype` parameter. * * @param string $output A space-separated list of language attributes. * @param string $doctype The type of HTML document (xhtml|html). */ return apply_filters( 'language_attributes', $output, $doctype );}Changelog
Introduced in 4.3.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 6.9.7 tag, from
src/wp-includes/general-template.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.