Using WordPress ‘language_attributes()’ PHP function

The language_attributes() WordPress PHP function generates language attributes for the html tag.

Usage

<!DOCTYPE html>
<html **<?php language_attributes(); ?>**>
<head>
...

Parameters

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

More information

See WordPress Developer Resources: language_attributes()

Examples

Default usage

This code snippet will generate the default language attributes for the html tag.

<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
...

XHTML doctype

This code snippet will generate language attributes for an XHTML document.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html <?php language_attributes('xhtml'); ?>>
<head>
...

HTML doctype

This code snippet will generate language attributes for an HTML document.

<!DOCTYPE html>
<html <?php language_attributes('html'); ?>>
<head>
...

Force language and text direction

This code snippet will generate language attributes and force the language to English and the text direction to left-to-right (LTR).

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
...

Add custom language attribute

This code snippet will add a custom language attribute (in this case, ‘en-GB’) to the html tag.

<!DOCTYPE html>
<html <?php language_attributes(); ?> lang="en-GB">
<head>
...