Current Language Class on the Body Tag
Adds classes like lang-fr and dir-rtl to the body tag, so you can adjust styles per language, whether the language comes from WPML, Polylang or the WordPress locale.
Opens WPIDE on
Code
add_filter( 'body_class', function ( $classes ) {
$code = strtok( str_replace( '_', '-', determine_locale() ), '-' );
if ( defined( 'ICL_LANGUAGE_CODE' ) && ICL_LANGUAGE_CODE ) {
$code = ICL_LANGUAGE_CODE;
} elseif ( function_exists( 'pll_current_language' ) && pll_current_language() ) {
$code = pll_current_language();
}
$classes[] = 'lang-' . sanitize_html_class( strtolower( $code ) );
$classes[] = is_rtl() ? 'dir-rtl' : 'dir-ltr';
return $classes;
} );
Notes
""
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.