Yes, it’s possible to have WordPress serve a different home page depending on the domain of the request. You’ll need to do some setup:
- Make sure every domain you’re planning on using is pointing to the server
- On the server, make sure the virtual host for each domain points to the WordPress directory. Alternatively you can just set the default web root and not have to worry about setting virtual hosts for each domain.
The first step is to make WordPress handle requests for any domain rather than just the domain it was installed on. You can do this by setting the WP_HOME
and WP_SITEURL
constants in the wp-config.php
file to use the host set in the request:
define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST']);
define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST']);
Next we have to determine what the homepage should be based on the request. We can do this by filtering the page_on_front
option:
add_filter('pre_option_page_on_front', function($page_id) {
switch($_SERVER['HTTP_HOST']) {
case 'www.domain1.com' :
case 'domain1.com' :
return $page_id; // Default
case 'www.domain2.com' :
case 'domain2.com' :
return 8; // Override
default :
exit; // We're not handling requests for this domain
}
});
The above code can be added to your plugin or your theme’s functions.php
file. Make sure the filter returns a valid page ID. The post type must be page
otherwise it will not work as expected.