apply_filters( 'language_attributes', string $output, string $doctype )

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


Parameters

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

Top ↑

Source

File: wp-includes/general-template.php. View all references

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


Top ↑

Changelog

Changelog
Version Description
4.3.0 Added the $doctype parameter.
2.5.0 Introduced.

Top ↑

User Contributed Notes

  1. Skip to note 1 content
    Contributed by Guillaume TURPIN

    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.