get_language_attributes()WP 4.3.0

Gets the language attributes for the <html> tag.

It generates:

  • dir="rtl" for right-to-left languages.
  • lang="..." for the HTML document.
  • xml:lang="..." for an XHTML document.

The language is determined through get_bloginfo() with the language parameter. The resulting string can be changed with the language_attributes filter.

Use language_attributes() to output the attributes.

1 time — 0.0018239 sec (very slow) | 50000 times — 0.40 sec (very fast)
Hooks from the function

Returns

String.

  • string - attributes separated by spaces.
  • '' - if no attribute was generated.

Usage

get_language_attributes( $doctype );
$doctype(string)

Document type: html or xhtml.

Default: html

Examples

0

#1 Getting language attributes

$attributes = get_language_attributes();

echo '<html ' . $attributes . '>';

For a Russian-language site, this will result in:

<html lang="ru-RU">

For a language with right-to-left writing direction:

<html dir="rtl" lang="ar">

Changelog

Since 4.3.0 Introduced.

get_language_attributes() code WP 7.0.4

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 );
}