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:

Getting the Current Language of your Site

The wpml_current_language 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 en shortcode anywhere on your site to display the code of the current language.

Getting the Current Language of a Post

The wpml_post_language_details 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.

Getting Other Languages of a Post

Read more about how to get the other languages of the current post.