Skip to content

Instantly share code, notes, and snippets.

@julien731
Created May 22, 2014 09:34
Show Gist options
  • Save julien731/cdf215283b6865fe8010 to your computer and use it in GitHub Desktop.
Save julien731/cdf215283b6865fe8010 to your computer and use it in GitHub Desktop.
class-theme-switcher.php
<?php
/**
* Theme Switcher.
*
* @package DBA_Components
* @author Julien Liabeuf <[email protected]>
* @license GPL-2.0+
* @link http://n2clic.com
* @copyright 2014 N2Clic
*/
class Theme_Switch {
public function __construct() {
$this->alternate = apply_filters( 'dba_theme_alternate', 'dba-mobile' );
// Make sure the Mobile_Detect class is loaded
if( !class_exists( 'Mobile_Detect' ) )
return;
// Instantiate Mobile_Detect
$detect = new Mobile_Detect();
// Check if current device is a mobile
if( $detect->isMobile() ) {
// We don't change for tablets
if( $detect->isTablet() )
return;
// Dynamically change theme
add_filter( 'template', array( $this, 'change_template' ) );
add_filter( 'stylesheet', array( $this, 'change_stylesheet' ) );
}
}
/**
* Change the template.
*
* @param string $template Current template
* @return string New template
*/
public function change_template( $template ) {
$theme = wp_get_theme( $this->alternate );
$alt = $theme->exists() ? $theme->template : $template;
return $alt;
}
/**
* Change the stylesheet.
*
* @param string $stylesheet Current stylesheet
* @return string New stylesheet
*/
public function change_stylesheet( $stylesheet ) {
$theme = wp_get_theme( $this->alternate );
$alt = $theme->exists() ? $theme->stylesheet : $stylesheet;
return $alt;
}
}
$dba_switch = new Theme_Switch();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment