How to Add WPML Language Code as Body Class in WordPress Without Plugins

In this tutorial, you’ll learn how to enhance your WordPress website by adding custom language body classes. This approach helps improve website localization, SEO, and user experience by providing unique class names based on the website’s language. We’ll achieve this without relying on plugins, ensuring a lightweight and efficient solution for your WordPress site.

Modify Your Theme’s Functions.php File

Start by accessing the functions.php file of your WordPress child theme. This file acts as the control center for customizing your theme’s features. You can make direct edits using the Theme Editor in your WordPress dashboard, or opt for more advanced editing using an external code editor alongside FTP (File Transfer Protocol). Insert the provided code snippet at the end of the file to incorporate the desired functionality.

function add_custom_language_body_class( $classes ) {
	if ( defined( 'ICL_LANGUAGE_CODE' ) ) {
		$language_class = 'lang_' . esc_attr( ICL_LANGUAGE_CODE );
		$classes[]      = $language_class;
	}
	return $classes;
}
add_filter( 'body_class', 'add_custom_language_body_class' );
Code language: PHP (php)

Explanation:
In this code snippet, the add_custom_language_body_class function checks if the constant ICL_LANGUAGE_CODE is defined by WPML. If it is, a custom class name is created by concatenating the prefix 'lang_' with the escaped and properly formatted language code obtained from ICL_LANGUAGE_CODE. This custom class is then added to the body classes array, ensuring unique and descriptive class names for each language.

Feel free to adjust the custom string ('lang_' in this case) to your preference. Make sure to choose a prefix that is meaningful and relevant to your website’s structure or content. After implementing this code, the resulting body classes will include class names like lang_dutch, lang_english, and so on, based on the current language code defined by WPML.

Save and Test Your Functions.php Modifications

After integrating the specified code snippet into your theme’s functions.php file, remember to save the file. Then, thoroughly test the applied features by interacting with your WordPress website. This crucial step ensures that the implemented functionalities are active and working seamlessly on your site.

That’s it! By following this tutorial, you’ve successfully added custom language body classes to your WordPress website without the need for plugins. This method enhances your website’s functionality, making it easier to style or target specific content based on the selected language. Understanding the code and its implications is essential for maintaining a well-organized and user-friendly WordPress website.

Leave your feedback and help us improve šŸ¶

We hope you found this article helpful! If you have any questions, feedback, or spot any errors, please let us know in the comments. Your input is valuable and helps us improve. If you liked this article, please consider sharing it with others. And if you really enjoyed it, you can show your support by buying us a cup of coffee ā˜•ļø or donating via PayPal šŸ’°.

More free knowledge, because why not?

Your thoughts matter, leave a reply šŸ’¬

Your email address will not be published. Required fields are marked *