我使用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使用多个子域,而对于这两个设置我只有一个设置。
发布于 2014-09-24 10:30:36
您可以覆盖wp-config.php文件中的管理设置。因此,如果您想要动态的东西,下面的工作应该是有效的:
//presumes server is set up to deliver over https
define('WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST']);
define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST']);这需要在行之前添加。
require_once(ABSPATH . 'wp-settings.php');否则,使用错误的URL(特别是主题文件)的某些内容可能会出现问题。
发布于 2014-09-26 01:23:47
我找到了另一种很好的方法来完成这项工作:
在我检查内核的源代码之后,我发现在每个选项上都有不同的过滤器,名为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文件,而且可以很容易地添加到主题或插件中。
发布于 2020-11-19 10:27:22
若要动态设置域以及协议 (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']);https://stackoverflow.com/questions/26014426
复制相似问题