最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Implementing a multilingual WordPress site by installing several instances of WordPress and redirecting

programmeradmin0浏览0评论

I am developing a website with WordPress which is going to support 4 languages. My primary language will be English and there will be 3 other languages.

For this reason, I have installed WordPress on public_html folder of my host and then created three other folders named fr, it and ar for French, Italian and Arabic respectively inside the public_html. Then I installed another instance of WordPress on each sub-directory. My goal was to have different admin panels for each language.

Finally, I tried redirecting based on IP of the visitor to a relevant website every time my website gets visited by a user. To implement this I used the following code:

function redirect_based_on_ip($country_code) {
echo($country_code);
$url = get_site_url();
if($country_code == "FR"){
    wp_redirect($url."/fr");
    exit;
}else if($country_code == "IT"){
    wp_redirect($url."/it");
    exit;
}else if($country_code == "DZ" || $country_code == "BH" || $country_code == "EG" || $country_code == "IQ" || $country_code == "JO" || $country_code == "KW" || $country_code == "LB" || $country_code == "LY" || $country_code == "OM" || $country_code == "QA" || $country_code == "SA" || $country_code == "SD" || $country_code == "SY" || $country_code == "AE" || $country_code == "YE"){
    wp_redirect($url."/ar");
    exit;
}else{
    wp_redirect($url);
    exit;
}

}

Please note that the $country_code parameter is the output of another function that gives me the country code of the visitor based on his/her IP which can be found here.

Unfortunately, I get the following error when I use this:

Warning: Cannot modify header information - headers already sent by (output started at /home/myHostUsername/public_html/wp-includes/class.wp-styles.php:290) in /home/myHostUsername/public_html/wp-includes/pluggable.php on line 1296

As I used this code inside my functions.php file of the primary website (EN), I found that by adding the following line to functions.php the error disappears but this time, the redirecting does not work correctly and the code always gets inside the else block which means English.

add_action( 'template_redirect', 'redirect_based_on_ip' );

I tested another way in which I installed every website in a single sub-directory (even primary one was installed inside public_html/en), and used an index.php file inside my public_html folder to do the redirecting and this way, everything was OK. But I don't want my primary version to be inside /en folder. How can I fix this?

By the way, please do not suggest me to use plugins as I don't want them.

发布评论

评论列表(0)

  1. 暂无评论