🚀 Is building a website too difficult? Let me guide you step by step—Learn about the 「Naibabiji WordPress Website Building Coaching Service」 →

Method for WordPress to automatically redirect URLs based on browser language

Manycompanies using WordPress to build Foreign Trade Independent Websitescreate websites in multiple languages, such as English and Chinese. At the same time, some Foreign Trade Independent Website administrators do not want their websites to be accessed by domestic users and wish to redirect domestic users to different URLs. We can use the browser language to determine the user's environment and then automatically redirect them to different URLs. The specific method is as follows: Insert the following code into your website Theme's header file (header.php)
Considering many are beginners and may not know how to modify code, it is recommended to install aCode Snippets PluginAfter installation, activate the plugin, edit the snippet named 'Example JavaScript snippet', and insert the code above below the comment /* write your JavaScript code here */.Then save to activate and enable the code. In the code above, you can add more languages, such as Brazilian, French, Portuguese, and then redirect to different URLs accordingly. The multi-language redirect code is as follows:
The above method referencesLiaoshen's article. Below are common browser language codes.
afAfrikaanssqAlbanian
ar-saArabic (Saudi Arabia)ar-iqArabic (Iraq)
ar-egArabic (Egypt)ar-lyArabic (Libya)
ar-dzArabic (Algeria)ar-maArabic (Morocco)
ar-tnArabic (Tunisia)ar-omArabic (Oman)
ar-yeArabic (Yemen)ar-syArabic (Syria)
ar-joArabic (Jordan)ar-lbArabic (Lebanon)
ar-kwArabic (Kuwait)ar-aeArabic (United Arab Emirates)
ar-bhArabic (Bahrain)ar-qaArabic (Qatar)
euBasquebgBulgarian
beBelarusiancaCatalan
zh-twChinese (Taiwan)zh-cnChinese (People's Republic of China)
zh-hkChinese (Hong Kong SAR)zh-sgChinese (Singapore)
hrCroatiancsCzech
daDanishnlDutch (Standard)
nl-beDutch (Belgium)enEnglish
en-usEnglish (United States)en-gbEnglish (United Kingdom)
en-auEnglish (Australia)en-caEnglish (Canada)
en-nzEnglish (New Zealand)en-ieEnglish (Ireland)
en-zaEnglish (South Africa)en-jmEnglish (Jamaica)
enEnglish (Caribbean)en-bzEnglish (Belize)
en-ttEnglish (Trinidad and Tobago)etEstonian
foFaroesefaPersian
fiFinnishfrFrench (Standard)
fr-beFrench (Belgium)fr-caFrench (Canada)
fr-chFrench (Switzerland)fr-luFrench (Luxembourg)
gdGaelic (Scottish)gaIrish
deGerman (Standard)de-chGerman (Switzerland)
de-atGerman (Austria)de-luGerman (Luxembourg)
de-liGerman (Liechtenstein)elGreek
heHebrewhiHindi
huHungarianisIcelandic
idIndonesianitItalian (Standard)
it-chItalian (Switzerland)jaJapanese
koKoreankoKorean (Hangul)
lvLatvianltLithuanian
mkMacedonian (North Macedonia)msMalay
mtMaltesenoNorwegian (Bokmål)
noNorwegian (Nynorsk)plPolish
pt-brPortuguese (Brazil)ptPortuguese (Portugal)
rmRomanshroRomanian
ro-moRomanian (Republic of Moldova)ruRussian
ru-moRussia (Republic of Moldova)szSami (Lappish)
srSerbian (Cyrillic)srSerbian (Latin)
skSlovakslSlovenian
sbSorbianesSpanish (Spain)
es-mxSpanish (Mexico)es-gtSpanish (Guatemala)
es-crSpanish (Costa Rica)es-paSpanish (Panama)
es-doSpanish (Dominican Republic)es-veSpanish (Venezuela)
es-coSpanish (Colombia)es-peSpanish (Peru)
es-arSpanish (Argentina)es-ecSpanish (Ecuador)
es-clSpanish (Chile)es-uySpanish (Uruguay)
es-pySpanish (Paraguay)es-boSpanish (Bolivia)
es-svSpanish (El Salvador)es-hnSpanish (Honduras)
es-niSpanish (Nicaragua)es-prSpanish (Puerto Rico)
sxSutusvSwedish
sv-fiSwedish (Finland)thThai
tsTsongatnTswana
trTurkishukUkrainian
urUrduveVenda
viVietnamesexhXhosa
jiYiddishzuZulu
Multiple language detection is implemented using PHP - if...elseif....else statements.
if (条件)
{
    if 条件成立时执行的代码;
}
elseif (条件)
{
    elseif 条件成立时执行的代码;
}
else
{
    条件不成立时执行的代码;
}
In addition to the method above, you can also insert the following code to achieve redirection for different languages.
");
else
print("");
If you are using a multilingual website built with translation plugins, the redirection function can usually be enabled directly in the plugin settings. Besides modifying code, you can also use domain name resolution and direct IP address targeting for redirection; some advanced DNS service providers support this feature.Other related articles previously written:

🚀 Still feeling confused after reading the tutorial? Let me guide you step-by-step instead.

「Naibabiji WordPress Website Building Coaching」 — From selecting a domain and purchasing hosting to installing themes and publishing posts, I「ll guide you through every step, helping you avoid detours and reach your goals directly.

👉 Learn about Website Building Coaching Service
🔒

Comments are closed

The comment function for this article is closed. If you have any questions, please feel free to contact us through other channels.

×
二维码

Scan to Follow