如何在WordPress中设置动态的"home"和"siteurl"? [英] How to set dynamic `home` and `siteurl` in WordPress?
问题描述
我使用locale
过滤器动态配置多语言设置.哪个获取子域名来确定语言.
I config the multi-language setting dynamically using the locale
filter. Which fetch the sub-domain name to determine the language.
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
).
That works for the index page, but when I redirect to another page, because of the settings of home
and siteurl
, it always redirects my site to the original one (www.mysite.com
).
所以我很好奇,想根据请求找到一种动态方式来过滤home
和siteurl
,因为我的网站可能会使用多个子域,而这两个设置只有一个设置
So I'm curious to find a dynamic way to filter the home
and siteurl
according to the request, because I might use more than one sub-domain for mysite and I have only one settings for the two settings.
推荐答案
您可以在wp-config.php文件中覆盖管理设置. 因此,如果您想要动态的东西,那么下面的方法应该起作用:
You can override the admin settings in the wp-config.php file. So if you want something dynamic, the following should work:
define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST']);
define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST']);
这需要在行之前添加
require_once(ABSPATH . 'wp-settings.php');
否则,您可能会使用错误的URL来处理某些内容,尤其是主题文件.
or else you may have problems with some content using the wrong URLs, especially theme files.
这篇关于如何在WordPress中设置动态的"home"和"siteurl"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!