I’ll preface this by saying I don’t think it’s the best of ideas, but in the interest of answering the question…
The general idea is to filter template
, option_template
, and option_stylesheet
in a plugin and return your desired template slug. The rest of the code is just setting and reading the cookie.
<?php
/**
* Plugin Name: WPD Theme Switcher
*/
class WPD_Theme_Switcher {
private $themes = array(
'twentythirteen',
'twentyfourteen',
'twentyfifteen'
);
private $current_theme="";
private $cookie="wpd_theme_switcher_cookie";
function __construct() {
if( empty( $this->current_theme ) && !isset( $_COOKIE[ $this->cookie ] ) ) {
$this->current_theme = $this->themes[ array_rand( $this->themes ) ];
setcookie( $this->cookie, $this->current_theme, time() + (10 * 365 * 24 * 60 * 60) );
} else {
$this->current_theme = $_COOKIE[ $this->cookie ];
}
// don't switch themes for admin requests
if( ! is_admin() ){
add_filter( 'template', array( $this, 'theme_switcher' ) );
add_filter( 'option_template', array( $this, 'theme_switcher' ) );
add_filter( 'option_stylesheet', array( $this, 'theme_switcher' ) );
}
}
function theme_switcher(){
return $this->current_theme;
}
}
new WPD_Theme_Switcher();