# Add the Page Language When the Theme Leaves It Out

> Sets the lang attribute on the page's html element from your site language when a theme forgets to print it, so screen readers pronounce your content in the right language.

- Type: PHP
- Runs: Frontend only
- Category: [Accessibility](https://wpide.com/library/category/accessibility/)
- Tags: language, screen-reader, theme
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/a11y-html-lang-fallback/

## Code

```php
add_action( 'wp_head', function () {
    printf(
        "<script>if(!document.documentElement.getAttribute('lang')){document.documentElement.setAttribute('lang',%s);}</script>\n",
        wp_json_encode( str_replace( '_', '-', determine_locale() ) )
    );
}, 0 );
```

## Notes

Themes that call language_attributes() already do this; the script only acts when the attribute is missing.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Add the Page Language When the Theme Leaves It Out" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
