Skip to content

Instantly share code, notes, and snippets.

@nb
Created August 27, 2010 14:09
Show Gist options
  • Save nb/553423 to your computer and use it in GitHub Desktop.
Save nb/553423 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Different theme
Description: Use a different theme on certain conditions
Author: Nikolay Bachiyski
Version: 1.0
*/
class Different_Theme {
function __construct() {
add_filter( 'stylesheet', array( &$this, 'theme_stylesheet' ) );
add_filter( 'option_stylesheet', array( &$this, 'theme_stylesheet' ) );
add_filter( 'template', array( &$this, 'theme_template' ) );
add_filter( 'option_template', array( &$this, 'theme_template' ) );
}
/**
* Decides whether the use a different theme from the current one.
*
* @return string|bool Returns a the relative path to the themes directory if a different
* theme should be used or false if the theme should not be changed
*/
function change_theme_to( $current_theme ) {
/*
Example:
if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && strpos( $_SERVER['HTTP_USER_AGENT'], 'iPhone' ) !== false ) {
return 'iphone-theme';
}
*/
return $current_theme;
}
function theme_template( $current_theme_template ) {
$new_template = $this->change_theme_to( $current_theme_template );
if ( $new_template != $current_theme_template ) {
// use parent template in case of a child theme
$theme_data = get_theme_data( get_theme_root(). "/$new_template/style.css" );
if ( $theme_data['Template'] ) {
$new_template = $theme_data['Template'];
}
}
return $new_template;
}
function theme_stylesheet( $current_theme_stylesheet ) {
return $this->change_theme_to( $current_theme_stylesheet );
}
}
$wp_plugin_different_theme = new Different_Theme;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment