Support » Developing with WordPress » defining theme for multiple domains in wp-config.php

  • Resolved Ivovic


    I’m creating a mobile version of my latest project the hard way.

    Basically, instead of doing detection and using a different style sheet, I want to allow increasingly-capable mobile users to view the site in its full glory by visiting the regular .com domain. Those who want to access the mobile version will do so via the .mobi domain of the same name.

    That way, the choice is theirs… and besides, the .mobi visitors will have almost certainly spent a large amount of time on the regular site first, so I’m not expecting any first-time mobile visitors.

    Before you jump in with the duplicate content deal, I want to point out that there’s a robots.txt blocking indexing of the .mobi domain so that google remains a happy chappy.

    Anyway, I’m doing the multiple domain bizzo using the following code in wp-config.php

    $domain = $_SERVER['SERVER_NAME'];
    $domain = str_replace('www.', '', $domain);
    if ($domain == '') {
       define('WP_SITEURL', '');
       define('WP_HOME', '');

    Now, I’d like to redefine the theme in use, so I can make a much lighter theme for the mobi domain, rather than just changing the style sheet.

    I tried to define the TEMPLATEPATH constant right there, but that resulted in wordpress trying to load the theme twice… once for the regular version, and once for the newly defined constant.

    Is there a way I can trick wordpress into re-defining the theme here in wp-config.php?… or perhaps somewhere where I can keep track of it just as easily, like index.php?

    I looked up the list of constants but as the two I’m actually using above are not listed there, I have little confidence in the list.

    Can anyone suggest an alternative to try?

    Thanks a bunch.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Well, I revisited this with a clearer head, and I’ve worked out why it wasn’t working.

    Here’s the section of wp-settings.php responsible for loading the functions.php.

    // Load functions for active theme.
    if ( TEMPLATEPATH !== STYLESHEETPATH && file_exists(STYLESHEETPATH . '/functions.php') )
    	include(STYLESHEETPATH . '/functions.php');
    if ( file_exists(TEMPLATEPATH . '/functions.php') )
    	include(TEMPLATEPATH . '/functions.php');

    as you can see there, because the second conditional isn’t an ‘elseif’ it tries to load the functions PHP *twice*, if TEMPLATEPATH doesn’t match STYLESHEETPATH.

    Of course this results in error central as existing functions are defined again.

    So, either one hacks that, or better yet, one defines both in wp-config.php:

    I’ve modified my function above, to make it simpler to add as many domains (and their themes) as you like.

    Put this into wp-config.php, under the $table_prefix definition, and edit to match your details:

    function domain_theme($domain) {
       define('WP_SITEURL', 'http://'.$domain);
       define('WP_HOME', 'http://'.$domain);
       define('TEMPLATEPATH', '/path/to/public_html/wp-content/themes/'.$domain);
       define('STYLESHEETPATH', '/path/to/public_html/wp-content/themes/'.$domain);
    $domain = $_SERVER['SERVER_NAME'];
    $domain = str_replace('www.', '', $domain);
    if ($domain == '') domain_theme($domain);

    Now, your main site’s theme folder can be called whatever you want, but your alternate site themes should be named after the domain they’re intended for, and bob’s your uncle. Just copy the if-statement at the end, to add additional domains safely.

    If you want to be a rogue about it, you can just assume that you’ve done everything correctly, and forget the error checking by changing the last line to:

    if ($domain !== '') domain_theme($domain);

    this will just assume that you’ve uploaded a theme for every domain you have parked at your site, and if you happen to access your site via a domain which doesn’t have a theme, you’ll probably get lots of error messages, so the first one is safer, but the second one is shorter if you have 15 domains parked at your site.

    I thoroughly suggest you don’t use this to stuff google full of duplicate content, but there are PLENTY of legitimate reasons you might want it, so here it is.

    if I could please request the thread to be renamed:
    “defining theme for multiple domains in wp-config.php”

    and for the thread to be moved to wp advanced that would be great.


    edit: thanks to the kind mod who moved this.

    Hi Ivovic,

    First of all thanks for a great piece of code, I have tried it and it works perfectly.

    I am stuck though on how to block the robots on the new parked domain, I have been all over google and found nothing on how to block the parked domain with robots.txt only how to do it with a permanent htaccess redirect which would defeat the whole object.

    Your wise advice would be greatly appreciated.

    Many thanks and best regards,


    Sorry for the delay in reply Mike, this thread fell off the first page in my profile, so I’m surprised I noticed it at all. If there’s something pressing regarding this I can help with, I can be reached via the contact page of my blog.

    What you want involves a tiny bit of creativity.

    what you would do is use rewriting rules (using mod_rewrite) in your .htaccess to present a different robots.txt depending on the domain used to access your site.

    In reality you’d have robots_allowed.txt and robots_blocked.txt, with rewrite rules to pull the appropriate file depending on whether you’re accessing via one domain or another.

    The rewrite engine allows you to specify a host as a variable, luckily, otherwise this would be a touch more difficult.

    I hope this helps… if it’s not sufficient, do let me know, and I’ll post an example once I’m back at my desk.

    I’m no rewriting guru… but here’s what I’m using myself to make this happen.

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^(www\.)?yoursite\.com$ [NC]
    RewriteRule ^robots.txt$ /robots_blocked.txt [NC,L]

    I put this above the wordpress section of the .htaccess, otherwise I suppose it would be clobbered.

    What it does is quite simple. Any request for robots.txt from your main site (, with a www or without) are passed to a regular old robots.txt file which may or may not be there.

    If any other domain (or subdomain) is used, the rule should trigger, thereby returning the contents of a file called robots_blocked.txt, under the guise of robots.txt

    you can test this pretty easily by making two text files (robots.txt and robots_blocked.txt) in your web root, and toying with it in your browser.

    I’m not a pro at this type of stuff so I just want to clarify for those of us that aren’t coding inclined of what to change.

    my understanding is that the following is what needs to be changed to match your theme location and website name?

    if ($domain == '') domain_theme($domain);


    seem like this has been resolved but here some more clean way to do it, u need to change the $themes to suit your settings then save it inside WP plugin dir and activated it (as plugin).

    Plugin Name: Domain Switch Theme
    Plugin URI: whatever
    Description: switch theme base on domain
    Version: 0.1dev
    Author: herself
    status: in development
    Author URI: whatever
    function maybe_switch_theme($host = false)
    	$host = ($host) ? $host : str_replace('www.', '', $_SERVER['SERVER_NAME']);
    	$themes = array(
    	'' => array('the-theme-dir','the-css-dir'),
    	'' => array('mobiletheme','mobiletheme'),
    	'' => array('default','default') );
    	if (isset($themes[$host])){
    		$themes = $themes[$host];

    caveat: your theme settings may have change but the site options like ‘home’ & ‘siteurl’ still pointing to the previous blog . You need to work on those.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘defining theme for multiple domains in wp-config.php’ is closed to new replies.