0

I am battling with the following:

    $themeId = "";
    $pathname = get_site_url();

    if($pathname == 'domain2.com'){
        $themeId =  "gold";}
    else{ 
        $themeId = "blue";}

    wp_register_style('theme-skin',THEMEURI . '/css/'.get_option($themeId).'-style.css', 3 , false, 'all');

So basically, I have a domain, domain1.com. It uses a blue theme for wordpress and hence the blue stylesheet which is: blue-style.css.

Now, I have another domain name, domain2.com, that uses the exact same wordpress installation. So I want to get the current url in the address bar, and based on that, change the color scheme, blue for domain1.com and gold for domain2.com.

But my code above does not work, it keeps using blue, no matter what url is visited - What am I doing wrong here?

Thanks

UPDATE: The original Default code

theme.css

        if(get_option(THEMESLUG."_17_style")){          
            wp_register_style('theme-skin',THEMEURI . '/css/'.get_option( THEMESLUG."_17_style").'-style.css', 3 , false, 'all'); //dark skin               
        }

Where the colors are specified: styling-options.php

array(
        "name"      => __("Theme Style",'rt_theme_admin'),
        "desc"      => __("Please choose a style for your theme.",'rt_theme_admin'),
        "id"        => THEMESLUG."_17_style",
        "options"   =>  array(
                        "blue"     => "Blue Style",
                        "purple"   => "Purple Style", 
                        "orange"   => "Orange Style",                       
                        "brown"   => "Brown Style",                                                 
                        "rose"   => "Rose Style",       
                        "green"   => "Green Style",     
                        "grey"   => "Grey Style",       
                        "gold"   => "Gold Style",                               
                        ),
        "default"   => "blue", 
        "type"      => "select"), 

SOLVED

Hi All. I did the following, and it now works:

if ( 'domain2.com' === $_SERVER['HTTP_HOST'] ) {

wp_register_style('theme-skin',THEMEURI . '/css/gold-style.css', 3 , false, 'all');

} else {

wp_register_style('theme-skin',THEMEURI . '/css/blue-style.css', 3 , false, 'all'); 

}

3 Answers 3

2

The Misconception

get_site_url() retrieves the site_url option from the database, as set under General Settings in the administrative backend / wp-admin.

If this is indeed

the exact same wordpress installation

it will always return the same thing, regardless of whether the location is reachable via one, two or five hundred domains.

You might want to check $_SERVER['HTTP_HOST'] instead.

if ( 'domain2.com' === $_SERVER['HTTP_HOST'] ) {
    $themeId =  "gold";
} else { 
    $themeId = "blue";
}

Real Voodoo

So far, your condition only works for the initial visit to your site.
Should you care for some real trickery and want the visitor to keep surfing your page under the initial domain: Check out this excellent answer by toscho.

6
  • 1
    aha- good catch, I feel stupid now!
    – Milo
    Jul 28, 2013 at 18:07
  • OK cool. I get the echo to work(echoing the domain name in the url), but it still does not register my colors...it keeps using the blue theme, which is set as the default by the theme. Please look at my updated answer, I added the relevant code which I edited for my purposes.I basically just changed the part shown here for theme.php. Do you perhaps know what I am doing wrong here? Jul 28, 2013 at 18:11
  • OK I got it to work, thanks to your helpful tip. Appreciate. See my updated answer to see how I got it to work... Jul 28, 2013 at 18:40
  • I am not so sure, @DextrousDave, whether "updating" the question so much that it seizes to reflect the initial one is actually such a good idea. Consider that, according to SE mechanics, neither questions, nor even more so answers are meant to aid the original poster only. Ideally, others finding your question(s) afterwards should equally be able to benefit from it. By removing any reference to get_site_url(), now Milo's and s_ha_dum's answers look really out of place. And my opening appears to be nonsense as well... Jul 28, 2013 at 19:04
  • Yeah, @Milo, I figured both of you would facepalm yourselves. ;) I guess, we're so in WP-mode sometimes, that we run the danger of missing the greater picture. I feel stupid at least once a day. And that's not necessarily a bad thing. Jul 28, 2013 at 19:09
0

If you refer to the Codex page for get_site_url():

Returns the 'siteurl' option with the appropriate protocol, 'https' if is_ssl() and 'http' otherwise.

so you should be checking a match for the domain with protocol, http://domain2.com

6
  • thank you for your Answer Milo. I added the http:// but it still does not work, I also added an alert just to check if it picks up the url in the first place, but it does not even display the alert when I am visiting domain2.com - see my updated answer(I updated the if statement) Jul 28, 2013 at 17:42
  • alert is a javascript function, not php. try echo $pathname to make sure it's what you are trying to match. I also don't see where you've added http:// in your updated code.
    – Milo
    Jul 28, 2013 at 17:49
  • OK cool. I corrected the alert as well (echo $pathname), and it still does not display any alert...Is there anything wrong with my code? I mean this is standard php in wordpress, why doesn't the echo alert at least work? Jul 28, 2013 at 18:01
  • echo $pathname before the if statement.
    – Milo
    Jul 28, 2013 at 18:08
  • ok cool, thanks. Johannes's suggestion works as far as the echoing goes, but my theme color does not change. see my update please Jul 28, 2013 at 18:14
0

get_site_url will display the complete URL-- for example, http://domain2.com. You can see that simply by dumping the output and looking at it. You are trying to match that against domain2.com. Obviously, it does not match and never will.

Try using:

    if($pathname == 'http://domain2.com'){
        $themeId =  "gold";}
    else{ 
        $themeId = "blue";}

But that could break if SSL is enabled, or if it is enabled in the future. Perhaps this would work better:

$pathname = parse_url(get_site_url());
echo $pathname['host'];
if($pathname['host'] == 'domain2.com'){
    $themeId =  "gold";
} else { 
    $themeId = "blue";
}
8
  • Thanks for your answer. I did try adding the http:// but still it does not work. I also added an alert to see if it picks up domain2.com, but I do not get an alert - see my updated code. I will try your second option and let you know Jul 28, 2013 at 17:45
  • What is $pathname exactly?
    – s_ha_dum
    Jul 28, 2013 at 17:48
  • it is just a variable I use for storing the url...Am I doing this wrong? Also, I tried your second option, and it does not work either. All it does is is shows 'domain1.com' on the top-left of the screen, when visiting both domain1.com and domain2.com Jul 28, 2013 at 17:56
  • No. I meant "what does the variable actually get set to?"
    – s_ha_dum
    Jul 28, 2013 at 18:00
  • not sure if I understand you correctly - I set the vairable $pathname to the url in the address bar - $pathname == get_site_url(); get_site_url() is a built in wordpress function...sorry if I am not on your page Jul 28, 2013 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.