Many
companies 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 a
Code 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 references
Liaoshen's article. Below are common browser language codes.
| af | Afrikaans | sq | Albanian |
| ar-sa | Arabic (Saudi Arabia) | ar-iq | Arabic (Iraq) |
| ar-eg | Arabic (Egypt) | ar-ly | Arabic (Libya) |
| ar-dz | Arabic (Algeria) | ar-ma | Arabic (Morocco) |
| ar-tn | Arabic (Tunisia) | ar-om | Arabic (Oman) |
| ar-ye | Arabic (Yemen) | ar-sy | Arabic (Syria) |
| ar-jo | Arabic (Jordan) | ar-lb | Arabic (Lebanon) |
| ar-kw | Arabic (Kuwait) | ar-ae | Arabic (United Arab Emirates) |
| ar-bh | Arabic (Bahrain) | ar-qa | Arabic (Qatar) |
| eu | Basque | bg | Bulgarian |
| be | Belarusian | ca | Catalan |
| zh-tw | Chinese (Taiwan) | zh-cn | Chinese (People's Republic of China) |
| zh-hk | Chinese (Hong Kong SAR) | zh-sg | Chinese (Singapore) |
| hr | Croatian | cs | Czech |
| da | Danish | nl | Dutch (Standard) |
| nl-be | Dutch (Belgium) | en | English |
| en-us | English (United States) | en-gb | English (United Kingdom) |
| en-au | English (Australia) | en-ca | English (Canada) |
| en-nz | English (New Zealand) | en-ie | English (Ireland) |
| en-za | English (South Africa) | en-jm | English (Jamaica) |
| en | English (Caribbean) | en-bz | English (Belize) |
| en-tt | English (Trinidad and Tobago) | et | Estonian |
| fo | Faroese | fa | Persian |
| fi | Finnish | fr | French (Standard) |
| fr-be | French (Belgium) | fr-ca | French (Canada) |
| fr-ch | French (Switzerland) | fr-lu | French (Luxembourg) |
| gd | Gaelic (Scottish) | ga | Irish |
| de | German (Standard) | de-ch | German (Switzerland) |
| de-at | German (Austria) | de-lu | German (Luxembourg) |
| de-li | German (Liechtenstein) | el | Greek |
| he | Hebrew | hi | Hindi |
| hu | Hungarian | is | Icelandic |
| id | Indonesian | it | Italian (Standard) |
| it-ch | Italian (Switzerland) | ja | Japanese |
| ko | Korean | ko | Korean (Hangul) |
| lv | Latvian | lt | Lithuanian |
| mk | Macedonian (North Macedonia) | ms | Malay |
| mt | Maltese | no | Norwegian (Bokmål) |
| no | Norwegian (Nynorsk) | pl | Polish |
| pt-br | Portuguese (Brazil) | pt | Portuguese (Portugal) |
| rm | Romansh | ro | Romanian |
| ro-mo | Romanian (Republic of Moldova) | ru | Russian |
| ru-mo | Russia (Republic of Moldova) | sz | Sami (Lappish) |
| sr | Serbian (Cyrillic) | sr | Serbian (Latin) |
| sk | Slovak | sl | Slovenian |
| sb | Sorbian | es | Spanish (Spain) |
| es-mx | Spanish (Mexico) | es-gt | Spanish (Guatemala) |
| es-cr | Spanish (Costa Rica) | es-pa | Spanish (Panama) |
| es-do | Spanish (Dominican Republic) | es-ve | Spanish (Venezuela) |
| es-co | Spanish (Colombia) | es-pe | Spanish (Peru) |
| es-ar | Spanish (Argentina) | es-ec | Spanish (Ecuador) |
| es-cl | Spanish (Chile) | es-uy | Spanish (Uruguay) |
| es-py | Spanish (Paraguay) | es-bo | Spanish (Bolivia) |
| es-sv | Spanish (El Salvador) | es-hn | Spanish (Honduras) |
| es-ni | Spanish (Nicaragua) | es-pr | Spanish (Puerto Rico) |
| sx | Sutu | sv | Swedish |
| sv-fi | Swedish (Finland) | th | Thai |
| ts | Tsonga | tn | Tswana |
| tr | Turkish | uk | Ukrainian |
| ur | Urdu | ve | Venda |
| vi | Vietnamese | xh | Xhosa |
| ji | Yiddish | zu | Zulu |
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:
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.