Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 610 Vote(s) - 3.65 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to set dynamic `home` and `siteurl` in WordPress?

#1
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');

---

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`).

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.
Reply

#2
I've found another pretty way to achieve the work:

After I checked for the source code of the kernel, I found that there are distinct filters called `option_xxx` on each options.

So, for my task, I tried to use the `option_siteurl` and `option_home` filter to hold that options to load, just to prevent the option to load, maintaining the `SERVER_NAME` it has:

function replace_siteurl($val) {
return 'http://'.$_SERVER['HTTP_HOST'];
}
add_filter('option_siteurl', 'replace_siteurl');
add_filter('option_home', 'replace_siteurl');

Using this way, it has no need to change the `wp_config.php` file, and can be easily add to a theme or a plugin.
Reply

#3
To **set dynamically the domain and as well as the protocol** (*http* or *https*), use:

```php
// 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']);
```
Reply

#4
You can override the admin settings in the wp-config.php file.
So if you want something dynamic, the following should work:

//presumes server is set up to deliver over https

define('WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST']);
define('WP_HOME', 'https://' . $_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.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through