apply_filters( ‘language_attributes’, string $output, string $doctype )

Filters the language attributes for display in the ‘html’ tag.


A space-separated list of language attributes.
The type of HTML document (xhtml|html).


return apply_filters( 'language_attributes', $output, $doctype );


4.3.0Added the $doctype parameter.

User Contributed Notes

  1. Skip to note 2 content

    Here is a function to force the locale of the HTML lang attribute without breaking the other attributes like dir

     * Forces the site locale for the HTML lang attribute.
     * @param string $locale the locale to set with the following format : "en-US"
     * @return void
    function wpdocs_force_site_locale( $locale = 'en-US' ) {
    	add_filter( 'language_attributes', function( $output ) use ( $locale ) {
    		// match the lang attribute and replace it with the new locale
    		$lang_regex = '/lang="([a-zA-Z-_]+)"/';
    		$output     = preg_replace( $lang_regex, 'lang="' . $locale . '"', $output );
    		// update the xml:lang attribute as well
    		$xml_lang_regex = '/xml:lang="([a-zA-Z-_]+)"/';
    		$output         = preg_replace( $xml_lang_regex, 'xml:lang="' . $locale . '"', $output );
    		return $output;
    	} );

You must log in before being able to contribute a note or feedback.