我使用locale
过滤器动态配置多语言设置。其中获取子域名以确定语言。
function load_custom_language($locale) {
// get the locale code according to the sub-domain name.
// en.mysite.com => return `en`
// zh.mysite.com => return `zh_CN`
// tw.mysite.com => return `zh_TW`
// etc..
}
add_filter('locale', 'load_custom_language');
适用于索引页面,但当我重定向到其他页面时,由于home
和siteurl
的设置,它始终将我的网站重定向到原始网站(www.mysite.com
)。
所以我很想找到一种根据请求过滤home
和siteurl
的动态方法,因为我可能会为mysite使用多个子域,而我只有一个设置对于这两个设置。
答案 0 :(得分:12)
您可以覆盖wp-config.php文件中的管理员设置。 因此,如果您想要一些动态的东西,以下内容应该有效:
define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST']);
define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST']);
这需要在行
之前添加require_once(ABSPATH . 'wp-settings.php');
否则您可能会遇到使用错误网址的某些内容的问题,尤其是主题文件。
答案 1 :(得分:4)
我找到了另一种实现这项工作的好方法:
在检查了内核的源代码后,我发现每个选项都有不同的过滤器名为option_xxx
。
因此,对于我的任务,我尝试使用option_siteurl
和option_home
过滤器来保存要加载的选项,只是为了防止加载选项,维护它SERVER_NAME
:
function replace_siteurl($val) {
return 'http://'.$_SERVER['HTTP_HOST'];
}
add_filter('option_siteurl', 'replace_siteurl');
add_filter('option_home', 'replace_siteurl');
使用这种方式,它无需更改wp_config.php
文件,并且可以轻松添加到主题或插件中。
答案 2 :(得分:0)
要动态设置域和协议( http 或 https ),请使用:
// Identify the relevant protocol for the current request
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https" : "http";
// Set SITEURL and HOME using a dynamic protocol.
define('WP_SITEURL', $protocol . '://' . $_SERVER['HTTP_HOST']);
define('WP_HOME', $protocol . '://' . $_SERVER['HTTP_HOST']);