How to Get Current Language In WordPress

How to Get Current Language In WordPressWhen you are developing a WordPress plugin, you may want to check the current language in WordPress using code. In this quick tutorial, I am going to show you how to get current language in WordPress and use it in your code.

Before we learn how to get current language in WordPress it helps to understand that you can set the WordPress language settings from the dashboard.

Get Current Language in WordPress

To change the language of WordPress or to view the set language in WordPress installation, you can go to settings and under the general settings, as shown on the image below, you can see the current language in WordPress.

Steps to Get Current Language in WordPress

To get the current language in WordPress you need to use the get_bloginfo() default WordPress function that returns the current language in WordPress if you use language as a parameter.

This function is designed to retrieve the data of the current site

get_bloginfo( string $show = '', string $filter = 'raw' )

The following are the possible values you can use in this function :

  •  ‘name’ – Site title (set in Settings > General)
  • ‘description’ – Site tagline (set in Settings > General)
  • ‘wpurl’ – The WordPress address (URL) (set in Settings > General)
  • ‘url’ – The Site address (URL) (set in Settings > General)
  • ‘admin_email’ – Admin email (set in Settings > General)
  • ‘charset’ – The “Encoding for pages and feeds” (set in Settings > Reading)
  • ‘version’ – The current WordPress version
  • ‘html_type’ – The content-type (default: “text/html”). Themes and plugins can override the default value using the ‘pre_option_html_type’ filter
  • ‘text_direction’ – The text direction determined by the site’s language. is_rtl() should be used instead
  • ‘language’ – Language code for the current site
  • ‘stylesheet_url’ – URL to the stylesheet for the active theme. An active child theme will take precedence over this value
  • ‘stylesheet_directory’ – Directory path for the active theme. An active child theme will take precedence over this value
  • ‘template_url’ / ‘template_directory’ – URL of the active theme’s directory. An active child theme will NOT take precedence over this value
  • ‘pingback_url’ – The pingback XML-RPC file URL (xmlrpc.php)
  • ‘atom_url’ – The Atom feed URL (/feed/atom)
  • ‘rdf_url’ – The RDF/RSS 1.0 feed URL (/feed/rdf)
  • ‘rss_url’ – The RSS 0.92 feed URL (/feed/rss)
  • ‘rss2_url’ – The RSS 2.0 feed URL (/feed)
  • ‘comments_atom_url’ – The comments Atom feed URL (/comments/feed)
  • ‘comments_rss2_url’ – The comments RSS 2.0 feed URL (/comments/feed)

As you can see there is the parameter of language that you can use to get the current language in WordPress as shown in the code sample below:

<?php

$currentLanguage = get_bloginfo('language');

if($currentLanguage == "id-ID"){
// Replace condition with your language code.

// Do something now

}

For the language codes, the following is the list of the codes you can replace in the conditional statement above:

af-ZA
am-ET
ar-AE
ar-BH
ar-DZ
ar-EG
ar-IQ
ar-JO
ar-KW
ar-LB
ar-LY
ar-MA
arn-CL
ar-OM
ar-QA
ar-SA
ar-SY
ar-TN
ar-YE
as-IN
az-Cyrl-AZ
az-Latn-AZ
ba-RU
be-BY
bg-BG
bn-BD
bn-IN
bo-CN
br-FR
bs-Cyrl-BA
bs-Latn-BA
ca-ES
co-FR
cs-CZ
cy-GB
da-DK
de-AT
de-CH
de-DE
de-LI
de-LU
dsb-DE
dv-MV
el-GR
en-029
en-AU
en-BZ
en-CA
en-GB
en-IE
en-IN
en-JM
en-MY
en-NZ
en-PH
en-SG
en-TT
en-US
en-ZA
en-ZW
es-AR
es-BO
es-CL
es-CO
es-CR
es-DO
es-EC
es-ES
es-GT
es-HN
es-MX
es-NI
es-PA
es-PE
es-PR
es-PY
es-SV
es-US
es-UY
es-VE
et-EE
eu-ES
fa-IR
fi-FI
fil-PH
fo-FO
fr-BE
fr-CA
fr-CH
fr-FR
fr-LU
fr-MC
fy-NL
ga-IE
gd-GB
gl-ES
gsw-FR
gu-IN
ha-Latn-NG
he-IL
hi-IN
hr-BA
hr-HR
hsb-DE
hu-HU
hy-AM
id-ID
ig-NG
ii-CN
is-IS
it-CH
it-IT
iu-Cans-CA
iu-Latn-CA
ja-JP
ka-GE
kk-KZ
kl-GL
km-KH
kn-IN
kok-IN
ko-KR
ky-KG
lb-LU
lo-LA
lt-LT
lv-LV
mi-NZ
mk-MK
ml-IN
mn-MN
mn-Mong-CN
moh-CA
mr-IN
ms-BN
ms-MY
mt-MT
nb-NO
ne-NP
nl-BE
nl-NL
nn-NO
nso-ZA
oc-FR
or-IN
pa-IN
pl-PL
prs-AF
ps-AF
pt-BR
pt-PT
qut-GT
quz-BO
quz-EC
quz-PE
rm-CH
ro-RO
ru-RU
rw-RW
sah-RU
sa-IN
se-FI
se-NO
se-SE
si-LK
sk-SK
sl-SI
sma-NO
sma-SE
smj-NO
smj-SE
smn-FI
sms-FI
sq-AL
sr-Cyrl-BA
sr-Cyrl-CS
sr-Cyrl-ME
sr-Cyrl-RS
sr-Latn-BA
sr-Latn-CS
sr-Latn-ME
sr-Latn-RS
sv-FI
sv-SE
sw-KE
syr-SY
ta-IN
te-IN
tg-Cyrl-TJ
th-TH
tk-TM
tn-ZA
tr-TR
tt-RU
tzm-Latn-DZ
ug-CN
uk-UA
ur-PK
uz-Cyrl-UZ
uz-Latn-UZ
vi-VN
wo-SN
xh-ZA
yo-NG
zh-CN
zh-HK
zh-MO
zh-SG
zh-TW
zu-ZA

Conclusion

In this post, we have looked at how to get the current language in WordPress. The take from this tutorial is the fact that you need to use the function get_bloginfo(). You can add this code to a hook and run it into any theme or plugin development.

Similar Articles