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.
Opens WPIDE on
Code
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 add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.