You don’t need multisite if WP is serving the same content for varying domains. You don’t even need a plugin, though there may be a few for this purpose none the less. You just need to add two lines of code to wp-config.php. See https://ww.wp.xz.cn/support/article/editing-wp-config-php/#wp_siteurl
Specifically the code for dynamically defining WP_SITEURL and WP_HOME.
Of course the second domain needs to be added to your hosting plan and it’s DNS nameservers set for your host. If your host is also your domain registrar they probably did this for you.
Thank you very much for your response! and I’m sorry I didn’t answer you before (Christmas season, you know).
I was seeing that option, it is certainly the same content on both websites, however there must be some different elements (logo, favicon, or About Us page, for example), and I don’t know if I can change these different elements with that option.
Thank you very much again.
-
This reply was modified 6 years, 5 months ago by
alberto3.
Not with that alone. But your theme can be customized to display different elements depending upon the value of WP_SITEURL through conditional if():else: control structures. It’s strongly recommended you create a child theme if you plan on customizing it. You can even conditionally apply entirely different CSS files, completely changing the appearance of the theme even if most content is identical.
if ('https://example.com' == WP_SITEURL ) {
// output something
} else {
// output something else
}
The “output something” could range from a specific img tag to an include statement that references an entirely different PHP file. It might be a little hacky, but you might even be able to conditionally load an entirely different theme. I can’t say I ever tried anything like that though.
Oh, it’s a good idea! thank you very much! I hadn’t thought of calling different css files from custom php, but it sounds good. I have a child theme, so I can make one with common elements and two for the logo and the small differences.
Now I just have to find a solution for the “About us” page. I will take a look to see if there is any option to call two different menus depending on the WP_SITEURL.
Thank you very much again!!
You’re welcome!
For varying page content like the about us page, create two separate pages named anything but “About Us”. “About Awesome Doodads” or whatever. Then hook the “pre_get_posts” action and check that the requested page name is “about-us”. If it is, and if WP_SITEURL == ” http://awesomedoodads.biz “, set the “pagename” query var to be “about-awesome-doodads”. The permalink slug will still be “about-us” but visitors will see the appropriate page.
If you like, you could define a boolean constant while you’re setting WP_SITEURL so it’s simpler to set up conditional structures. You can then more easily use ternary operators. Say you defined IS_DOO as true for the awesome doodads domains. Then to conditionally load one menu or the other you can do:
wp_nav_menu(['menu'=> IS_DOO ? 'doodads':'foobar';,]);
assuming “doodads” and “foobar” are menus defined in the admin area. A lot easier than formal if/else structures, yet still readable, provided you understand ternary operators, which are just shorthand if/else structures. You can still use IS_DOO in more formal structures as well since it just resolves to true or false.
Thanks a lot for the explanation, there is a whole world of programming out there. At first I had in mind something more simple as a plugin, but I will look deeply at all that coding. Now I have a way to go, thanks again!! Have a great day!!
Happy to help. Sorry if I led you down a programming rabbit hole! First just the domain was different. Then, oh, we need a different logo. Then, a different page. I’m not complaining, it’s you that needs to figure this stuff out š With these sorts of things there is almost always a coding solution. It may seem like a lot of fuss, but once it’s in place it’s all automatic and painless.
I think it’s well worth learning a few basic things like this. It’s empowering to have that kind of control over your site.
Yeah, I agree with you!
I am testing the WP_SITEURL and WP_HOME, but it seems to not work properly.
Sometimes the links from DomainONE point to the DomainTWO, and the links in the DomainTWO points to the DomainONE. Other times it works well, it seems to be random.
This is my code on wp-config.php:
define('WP_SITEURL', 'https://'.$_SERVER['HTTP_HOST']);
define('WP_HOME', 'https://'.$_SERVER['HTTP_HOST']);
I thought it could be because of the website cache or browser cache, so I tried to open each domain in different browsers, private browsing, emptying cache, deactivating cache plugins… but nothing works.
Do you know how I can do to solve this issue? I don’t want users visiting the domainONE to see the domainTWO’s logo, and vice versa.
I also tried the ternary operator to change the logo, I don’t know if it’s ok, it seems to work:
('https://domainone.com' == WP_SITEURL ) ? ($is_dam = true) : ($is_dam = false);
($is_dam) ? ($logo = '/logo-domainone.png') : ($logo = '/logo-domaintwo.png');
Thank you very much!
Random glitches are hard to debug. Unless you can find a way to reliably replicate the issue there is no way to know if any corrective measure actually works or not. Permalinks and such are filterable so themes and plugins can corrupt our carefully laid plan. For example, they may get the domain from the DB table values instead of what WP is reporting based on the constants we defined. Maybe we could filter the get_option() call for siteurl and home ourselves and preempt any attempts to get values from the DB.
add_filter('option_siteurl', function() { return WP_SITEURL; }, PHP_INT_MAX );
add_filter('option_home', function() { return WP_HOME; }, PHP_INT_MAX );
Add to your theme’s functions.php
The way you handled the custom logo is all right (it works!), but $is_dam has limited scope. You can only use it in so many places. Constants have global scope, you can use them anywhere. An improvement would be to define IS_DAM as a constant right in wp-config.php, right after WP_SITEURL and WP_HOME are defined. Then you can use IS_DAM anywhere without concern for scope.
Thank you very much for your answer! I didn’t know I could define custom constant in wp-config. This is much easier!
Regarding the cache, I have tried adding your code to functions.php but that is not the problem. The problem is cache plugins. I had deactivated the plugins but had not deleted the wp-content->cache folder. By deleting the folder the two domains work perfectly, even in the same browser.
I have asked the plugin developers to see if I can solve the cache problem. I will try other plugins or try to do it manually.
If I find a solution I will copy it here in case it can be helpful to another person with the same issue in the future.
Thank you very much again for your help.