PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, May 16, 2022

[FIXED] How to set dynamic `home` and `siteurl` in WordPress?

 May 16, 2022     hook, php, wordpress     No comments   

Issue

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.


Solution

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.



Answered By - Steve
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing