How to automatically switch Wordpress themes on the fly

February 20, 2010

Submitted by: Fayçal

Category: Computers

3,465 views

Want to switch between two themes according to a specific criteria ?

Edit : Try this plugin if your criteria is based on an URI pattern

Just add and activate this small plugin by creating a file wp_theme_switcher.php in Wordpress plugins directory with the following content:

<?php
/*
 Plugin Name: WP Theme Switcher
 Description: Automatically switch Wordpress themes on the fly
 Version: 2.0
 Author: Fayçal Tirich
 Author URI: http://agafix.org
 Plugin URI: http://agafix.org/how-to-automatically-switch-wordpress-themes-onthefly/
 */
 
class WP_Theme_Switcher {
	public $current_theme;
	public $theme_to_apply;
	public $current_page;
 
	public function __construct() {
		$this->current_page = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
		$current_theme_data = get_theme(get_current_theme());
		$this->current_theme = $current_theme_data["Template"];
		$this->theme_to_apply = $this->current_theme ;
	}
 
	public function get_theme_to_apply(){
		$default_theme = "default";
		$alternatif_theme = "classic";
		//your criteria
		if ($_GET['cat']==3){
			$this->theme_to_apply = $alternatif_theme;
		}
		return $this->theme_to_apply ;
	}
 
	public function switch_theme_when_it_has_to_be(){
		add_filter( 'template', array(&$this, 'get_theme_to_apply') );
		add_filter( 'stylesheet', array(&$this, 'get_theme_to_apply') );
	}
}
 
$wp_theme_switcher = new WP_Theme_Switcher();
add_action('setup_theme', array(&$wp_theme_switcher,'switch_theme_when_it_has_to_be'));
?>

For example imagine you have two themes : my_ar_theme for Arabic content and my_jp_theme for Japanese one and you want Wordpress to be able to switch between them on the base of the blog URI (eg : if the URI contains a “/ar/” substring like for www.example.org/ar/post or a “/jp/” like for www.example.org/jp/post)

In this case the code to past will be

<?php
/*
 Plugin Name: WP Theme Switcher
 Description: Automatically switch Wordpress themes on the fly
 Version: 2.0
 Author: Fayçal Tirich
 Author URI: http://agafix.org
 Plugin URI: http://agafix.org/how-to-automatically-switch-wordpress-themes-onthefly/
 */
 
class WP_Theme_Switcher {
	public $current_theme;
	public $theme_to_apply;
	public $current_page;
 
	public function __construct() {
		$this->current_page = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
		$current_theme_data = get_theme(get_current_theme());
		$this->current_theme = $current_theme_data["Template"];
		$this->theme_to_apply = $this->current_theme ;
	}
 
	public function get_theme_to_apply(){
		$default_theme = "default";
		$alternatif_theme = "classic";
 
		//your criteria
		$pattern="/ar/";
		if (!empty($pattern)){
			// Credits to Yann Dubois 
			if( preg_match("/" . preg_replace( '|/|', '/', $pattern ) . "/i", $this->current_page ) ) {
				$this->theme_to_apply = $alternatif_theme;
			}
		}
		return $this->theme_to_apply ;
	}
 
	public function switch_theme_when_it_has_to_be(){
		add_filter( 'template', array(&$this, 'get_theme_to_apply') );
		add_filter( 'stylesheet', array(&$this, 'get_theme_to_apply') );
	}
}
 
$wp_theme_switcher = new WP_Theme_Switcher();
add_action('setup_theme', array(&$wp_theme_switcher,'switch_theme_when_it_has_to_be'));
?>

Another example is to switch the theme only some days a week as asked Brooke in comments

<?php
/*
 Plugin Name: WP Theme Switcher
 Description: Automatically switch Wordpress themes on the fly
 Version: 2.0
 Author: Fayçal Tirich
 Author URI: http://agafix.org
 Plugin URI: http://agafix.org/how-to-automatically-switch-wordpress-themes-onthefly/
 */
 
class WP_Theme_Switcher {
	public $current_theme;
	public $theme_to_apply;
	public $current_page;
 
	public function __construct() {
		$this->current_page = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
		$current_theme_data = get_theme(get_current_theme());
		$this->current_theme = $current_theme_data["Template"];
		$this->theme_to_apply = $this->current_theme ;
	}
 
	public function get_theme_to_apply(){
		$default_theme = "default";
		$alternatif_theme = "classic";
		$alternatif_theme_days = array('Monday','Friday','Sunday');
 
		//your criteria
		if (in_array(date('l'), $alternatif_theme_days)) {
		$this->theme_to_apply = $alternatif_theme;
		}
 
		return $this->theme_to_apply ;
	}
 
	public function switch_theme_when_it_has_to_be(){
		add_filter( 'template', array(&$this, 'get_theme_to_apply') );
		add_filter( 'stylesheet', array(&$this, 'get_theme_to_apply') );
	}
}
 
$wp_theme_switcher = new WP_Theme_Switcher();
add_action('setup_theme', array(&$wp_theme_switcher,'switch_theme_when_it_has_to_be'));
?>

You can download this last example via this link

The same goes for more than two themes, just define your concerned themes, change your criteria, activate the plugin and watch the Wordpress magic flexibility !

If you liked this, please share it at your favorite sites:
  • Digg
  • Reddit
  • del.icio.us
  • Wikio
  • Facebook
  • Google
  • Technorati
  • TwitThis
  • MySpace
  • N4G
  • NewsVine
  • StumbleUpon
  • BlogMemes
  • Blogsvine
  • blogtercimlap
  • DotNetKicks
  • eKudos
  • Faves
  • Fleck
  • Scoopeo
  • Socialogs
  • Upnews
  • Yigg
  • E-mail this story to a friend!
  • Mixx

30 Responses :D Pretty Cool

Leave a Reply

:D :) :cry: :( 8O :twisted: :!: :vangry: :XO: :up: ;) :mrgreen: :halo: :kiss: :roll: :? 8) :evil: :oops: :| :?: :x :$: