Alfred Huang Alfred Huang - 2 months ago 13
PHP Question

How to set dynamic `home` and `siteurl` in WordPress?

I config the multi-language setting dynamically using the

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.
// => return `en`
// => return `zh_CN`
// => return `zh_TW`
// etc..
add_filter('locale', 'load_custom_language');

That works for the index page, but when I redirect to another page, because of the settings of
, it always redirects my site to the original one (

So I'm curious to find a dynamic way to filter the
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.


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']);

This needs to added before the line

require_once(ABSPATH . 'wp-settings.php');

or else you may have problems with some content using the wrong URLs, especially theme files.