1

I have a wordpress theme, but I use this single installation for 2 domains. Both domains point to the same installtion.

Now, I want to use jQuery, based on the domain name in the address bar, to change the color scheme of the WP Site.

Now, in the WP-dashboard, you can change the color of the theme. Now this interface where you can choose the theme color, is defined in the follwing code inside a rt_styling_options.php file:

    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"),  

Here is a screenshot of the WP-Dashboard where you can choose this color:

enter image description here

Question:

How do I change the value of that color based on the domain name in the address bar?

It will go something like this:

var pathname = window.location.pathname;

if (pathname == 'domain2.com'){

    change the value of the theme color   

}

Additonal Info (important):

Here is the php file (theme.php) and relevant function where this color value above is called into: ( see if(get_option(THEMESLUG."_17_style")){ )

function load_styles(){


        wp_register_style('theme-reset', THEMEURI . '/css/rt-css-framework.css', 1 , false, 'all');
        wp_register_style('theme-style-all',THEMEURI . '/css/style.css', 2 , false, 'all');

        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               
        }

        wp_register_style('prettyPhoto',THEMEURI . '/css/prettyPhoto.css', 5 , false, 'screen');

        if(get_option(THEMESLUG."_font_face")){
            wp_register_style('rtfontface',THEMEURI . '/css/fontface.css', 100 , false, 'all');
        }       


        wp_enqueue_style('theme-reset');
        wp_enqueue_style('theme-style-all');  
        wp_enqueue_style('rtfontface'); 
        wp_enqueue_style('prettyPhoto');         
        wp_enqueue_style('jquery-colortip', THEMEURI . '/css/colortip-1.0-jquery.css');      
        wp_enqueue_style('jquery-jcarousel', THEMEURI . '/css/jcarousel.css');  
        wp_enqueue_style('jquery-flexslider', THEMEURI . '/css/flexslider.css');
        wp_enqueue_style('jquery-nivoslider', THEMEURI . '/css/nivo-slider.css');
        wp_enqueue_style('jquery-nivoslider-theme', THEMEURI . '/css/nivo-default/default.css'); 
        wp_enqueue_style('theme-skin');

        wp_register_style('theme-ie7',THEMEURI . '/css/ie7.css', 6 , false, 'screen');
        $GLOBALS['wp_styles']->add_data( 'theme-ie7', 'conditional', 'IE 7' );
        wp_enqueue_style('theme-ie7');

        wp_register_style('theme-ie8',THEMEURI . '/css/ie8.css', 6 , false, 'screen');
        $GLOBALS['wp_styles']->add_data( 'theme-ie8', 'conditional', 'IE 8' );
        wp_enqueue_style('theme-ie8');


        wp_register_style('theme-style',get_bloginfo( 'stylesheet_url' ), 4 , false, 'all'); //WP default stylesheet 
        wp_enqueue_style('theme-style');

        if (class_exists( 'Woocommerce' ) ) { //woocommerce style for rt-theme
            global $woocommerce, $suffix;

            wp_enqueue_style( 'rt-woocommerce-styles', THEMEURI.'/rt-woocommerce/woocommerce.css');

        }
    }



UPDATE: @Patrick

•Changes in theme.php

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

        if($pathname == 'domain2.com')
       $themeId =  THEMESLUG."_17_style2";
        else 
           $themeId = THEMESLUG."_17_style";

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

NOTE: I removed this from the original theme.php file:

if(get_option(THEMESLUG."_17_style")){  .....}

•Changes in rt_styling_options.php: just added the following right after the first set, as you specified

array(
        "name"      => __("Theme Style",'rt_theme_admin'),
        "desc"      => __("Please choose a style for your theme.",'rt_theme_admin'),
        "id"        => THEMESLUG."_17_style2",
        "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"   => "gold", 
        "type"      => "select"),  
2
  • 1
    you would use pure php for this as js cant change a php var Commented Jul 24, 2013 at 20:14
  • yeah of course...did not think straight there Commented Jul 24, 2013 at 20:19

2 Answers 2

1

You definitely can't change those Wordpress PHP array values with JQuery. (JQuery is client side while PHP is server side).

What you can do instead is use JQuery to target the CSS and HTML elements based on the URL Path name... This won't be easy though because you'll have to take into consideration all the elements affected by the theme.

OR, you'll have to use PHP to do all the logic between switching themes based on the URL Path.

Looks like you can use <?php get_site_url(); ?> to get the URL Path with PHP in Wordpress. See more here: http://codex.wordpress.org/Function_Reference/get_site_url

Update

It seems like you can change your current theme by making direct calls to the wordpress SQL database:

function switchTheme($theme) {
  global $wpdb;
  if (isset($theme)) {
    $queries = array("UPDATE wp_options SET option_value = 'default'
                      WHERE option_name = 'template';",
                     "UPDATE wp_options SET option_value = 'default'
                      WHERE option_name = 'stylesheet';",
                     "UPDATE wp_options SET option_value = 'default'
                      WHERE option_name = 'current_theme';");
    foreach ($queries as $query){
        $wpdb->query($query);
    }
  }
}

option_value is where you would put your theme name.

Sources:

http://designgala.com/how-to-change-wordpress-theme-directly-from-database/ http://www.wprecipes.com/wordpress-trick-change-theme-programatically

Also there are the switch_theme and update_option functions from Wordpress.

Similar to the above SQL queries, you can use update_option to update your theme like:

update_option('template', 'theme_name');
update_option('stylesheet', 'theme_name');
update_option('current_theme', 'theme_name');

http://codex.wordpress.org/Function_Reference/switch_theme http://codex.wordpress.org/Function_Reference/update_option

Sign up to request clarification or add additional context in comments.

3 Comments

yeah of course! Didn't think about that. But how will I pass the url path into php? Of course there must be a php function I can run when the website is requested from the webserver by the browser, based on the url the browser passed to the webserver?
PHP is capable of getting the URL Path usually. Did you see this? codex.wordpress.org/Function_Reference/get_site_url
Updated answer, might give you some help on changing the actual theme through PHP/SQL.
0

Modify the template to have more then one color option, give each a unique id

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"),  
array(
    "name"      => __("Theme Style",'rt_theme_admin'),
    "desc"      => __("Please choose a style for your theme.",'rt_theme_admin'),
    "id"        => THEMESLUG."_17_style2",
    "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"),  

And then in theme.php

$themeId = "";
    if($pathname == 'domain2.com')
       $themeId =  THEMESLUG."_17_style2";
    else 
       $themeId = THEMESLUG."_17_style";

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

10 Comments

thank you Patrick. It makes sense, but is 'pathame' actually a php function for getting the url path? Isn't it REQUEST_URI?
No pathname isn't a PHP function it's the variable you set with javascript which Patrick overlooked. Yup usually it's $_SERVER['REQUEST_URI'], but Wordpress should have their own function too.
yeah, as projeqht pointed out, it is <?php get_site_url(); ?>
yea simply forgot to put the $ before the pathname and you dont set it from javascript, you just get it in the php like with the get_site_url function or just use the get_site_url in the if statement.
could you please update your answer with the right url call using the <?php get_site_url(); ?>...I tried <?php get_site_url($pathname); ?> but breaks my site
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.