Skip Navigation

When developing your site, one common thing you may want to do is get the current language using code. There are two WPML hooks that offer similar ways of doing this, but with important distinctions.

WPML offers two similar hooks for getting the current language:

wpml_current_language

This hook is the recommended way to get the current language of your site. One way to use this hook is to create a shortcode that returns the current language as the 2-letter language code.

function get_language_shortcode() {
    return apply_filters( 'wpml_current_language', null );
}
add_shortcode( 'language', 'get_language_shortcode' );

With this code, you can use the [language] shortcode anywhere on your site to display the code of the current language.

wpml_post_language_details

This hook is used to get language details of a post using the post ID. This hook returns the language code, post locale, language text direction, language name and native name, and if the current language is different to the post’s default language. This hook can be useful if you want to create a custom language switcher.

For more information and an example, see the documentation page about the wpml_post_language_details hook.


Need help?

Locales and language codes support topics

ICL_LANGUAGE_CODE related support