首先,我要说,我认为这不是最好的想法,但为了回答这个问题。。。
总体思路是过滤template
, option_template
, 和option_stylesheet
并返回所需的模板段塞。其余的代码只是设置和读取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();