如何解决通过安装多个 WordPress 实例并重定向
我正在使用 WordPress 开发一个网站,该网站将支持 4 种语言。我的主要语言是英语,还有其他 3 种语言。
出于这个原因,我在主机的 public_html 文件夹上安装了 WordPress,然后在 public_html 中创建了另外三个文件夹,分别名为 fr、it 和 ar 分别代表法语、意大利语和阿拉伯语。然后我在每个子目录上安装了另一个 WordPress 实例。我的目标是为每种语言设置不同的管理面板。
最后,每次用户访问我的网站时,我都尝试根据访问者的 IP 重定向到相关网站。为了实现这一点,我使用了以下代码:
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;
}
}
请注意,$country_code 参数是另一个函数的输出,该函数根据访问者的 IP 提供访问者的国家/地区代码,该 IP 可以在 here 中找到。
不幸的是,当我使用它时出现以下错误:
警告:无法修改标头信息 - 标头已由 /home/myHostUsername/public_html/wp-includes 中的(输出开始于 /home/myHostUsername/public_html/wp-includes/class.wp-styles.php:290)发送/pluggable.php 第 1296 行
当我在主网站 (EN) 的functions.php 文件中使用此代码时,我发现通过将以下行添加到functions.php 中,错误消失了,但这一次,重定向无法正常工作并且代码总是进入 else 块,这意味着英语。
add_action( 'template_redirect','redirect_based_on_ip' );
我测试了另一种将每个网站安装在单个子目录中的方法(即使是主目录也安装在 public_html/en 中),并使用我的 public_html 文件夹中的 index.php 文件进行重定向,这样,一切正常。但我不希望我的主要版本在 /en 文件夹中。我该如何解决这个问题?
顺便说一句,请不要建议我使用插件,因为我不想要它们。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。