Using WordPress ‘get_language_attributes()’ PHP function

The get_language_attributes() WordPress PHP function retrieves the language attributes for the ‘html’ tag.

Usage

echo get_language_attributes('html');

Parameters

  • $doctype (string) Optional: The type of HTML document. Accepts ‘xhtml’ or ‘html’. Default is ‘html’.

More information

See WordPress Developer Resources: get_language_attributes()

Examples

Default Usage

// Adds language attributes to the 'html' tag in the header
<head <?php language_attributes(); ?>>

XHTML Document Type

// Adds language attributes to the 'html' tag in the header for an XHTML document
<head <?php echo get_language_attributes('xhtml'); ?>>

HTML Document Type

// Adds language attributes to the 'html' tag in the header for an HTML document
<head <?php echo get_language_attributes('html'); ?>>

Custom Language Attributes

// Define custom language attributes
function my_custom_language_attributes($doctype) {
  return 'lang="en-US" dir="ltr"';
}
add_filter('language_attributes', 'my_custom_language_attributes', 10, 1);

// Use custom language attributes in the 'html' tag
<head <?php language_attributes(); ?>>

Modify Existing Language Attributes

// Modify the existing language attributes to add 'data-custom-attribute'
function my_modify_language_attributes($output, $doctype) {
  $output .= ' data-custom-attribute="custom-value"';
  return $output;
}
add_filter('language_attributes', 'my_modify_language_attributes', 10, 2);

// Use the modified language attributes in the 'html' tag
<head <?php language_attributes(); ?>>