{"id":11756113,"date":"2022-07-28T07:29:54","date_gmt":"2022-07-28T07:29:54","guid":{"rendered":"https:\/\/wpml.org\/?post_type=faq&#038;p=11756113"},"modified":"2023-09-06T10:43:37","modified_gmt":"2023-09-06T10:43:37","slug":"how-to-get-current-language-with-wpml","status":"publish","type":"faq","link":"https:\/\/wpml.org\/it\/faq\/how-to-get-current-language-with-wpml\/","title":{"rendered":"How can I get the current language with WPML?"},"content":{"rendered":"<p class=\"lead\">You can get the current language by using two WPML hooks, <a href=\"https:\/\/wpml.org\/it\/wpml-hook\/wpml_current_language\/\">wpml_current_language<\/a>, and <a href=\"https:\/\/wpml.org\/it\/wpml-hook\/wpml_post_language_details\/\">wpml_post_language_details<\/a>.<\/p>\n<p>The wpml_current_language is ideal for getting the site&#8217;s current language, while the wpml_post_language_details provides detailed information about a post&#8217;s language attributes.<\/p>\n<h2>How can I get the current language of my site?<\/h2>\n<p>The <strong>wpml_current_language<\/strong> hook is the recommended way to get the current language <strong>of your site<\/strong>. One way to use this hook is to create a shortcode that returns the current language as the 2-letter language code.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfunction get_language_shortcode() {\r\n    return apply_filters( 'wpml_current_language', null );\r\n}\r\nadd_shortcode( 'language', 'get_language_shortcode' );\r\n<\/pre>\n<p>With this code, you can use the <code>it<\/code> shortcode anywhere on your site to display the code of the current language.<\/p>\n<div class=\"alert alert-warning\" role=\"alert\">The constant <strong>ICL_LANGUAGE_CODE<\/strong> works in a similar way. However, please note that this is a deprecated method for getting the current language and does not work in all cases. We strongly recommend using the <strong>wpml_current_language<\/strong> hook instead.<\/div>\n<h2>How can I get the current language of a post?<\/h2>\n<p>The <strong>wpml_post_language_details<\/strong> hook is used to get language details <strong>of a post<\/strong> 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\u2019s default language. This hook can be useful if you want to create a custom language switcher.<\/p>\n<p>For more information and an example, see the documentation page about the <a href=\"https:\/\/wpml.org\/it\/wpml-hook\/wpml_post_language_details\/\"><strong>wpml_post_language_details<\/strong> hook<\/a>.<\/p>\n<h2>How can I get other languages of a post?<\/h2>\n<p>Read more about <a href=\"https:\/\/wpml.org\/it\/faq\/how-to-get-other-languages-of-current-post\/\">how to get the other languages of the current post<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can get the current language by using two WPML hooks, wpml_current_language, and wpml_post_language_details. <\/p>\n","protected":false},"author":353313,"featured_media":0,"menu_order":0,"template":"","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"ep_exclude_from_search":false,"footnotes":""},"faq-category":[26137],"class_list":["post-11756113","faq","type-faq","status-publish","hentry","faq-category-how-to"],"_links":{"self":[{"href":"https:\/\/wpml.org\/it\/wp-json\/wp\/v2\/faq\/11756113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpml.org\/it\/wp-json\/wp\/v2\/faq"}],"about":[{"href":"https:\/\/wpml.org\/it\/wp-json\/wp\/v2\/types\/faq"}],"author":[{"embeddable":true,"href":"https:\/\/wpml.org\/it\/wp-json\/wp\/v2\/users\/353313"}],"version-history":[{"count":7,"href":"https:\/\/wpml.org\/it\/wp-json\/wp\/v2\/faq\/11756113\/revisions"}],"predecessor-version":[{"id":14351079,"href":"https:\/\/wpml.org\/it\/wp-json\/wp\/v2\/faq\/11756113\/revisions\/14351079"}],"wp:attachment":[{"href":"https:\/\/wpml.org\/it\/wp-json\/wp\/v2\/media?parent=11756113"}],"wp:term":[{"taxonomy":"faq-category","embeddable":true,"href":"https:\/\/wpml.org\/it\/wp-json\/wp\/v2\/faq-category?post=11756113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}