Wordpress使用一个选项来存储当前活动的子主题。此选项命名为\'stylesheet\'
.
使用过滤器\'pre_option_{$option_name}\'
您可以更改WP获得的每个选项。
所以,在你的情况下,你应该\'pre_option_stylesheet\'
过滤,使用一些逻辑来检索当前季节并返回所需的子主题。这个工作流应该在插件中完成(在主题和子主题之前加载),这样您就可以确保它工作正常。
插件代码示例:
<?php
/**
* Plugin Name: Seasoned Child Theme
* Plugin URI: http://wordpress.stackexchange.com/questions/114538/conditionally-load-child-themes
* Author: G.M.
* Author URI: http://wordpress.stackexchange.com/users/35541/
*/
add_action(\'pre_option_stylesheet\', function() {
$seasons_themes = array(
// this are the child theme names (the folder name) related to season
\'child_winter\', \'child_spring\', \'child_summer\', \'child_autumn\'
);
$m = intval( date(\'n\') );
if ( $m < 3 || $m == 12 ) {
$theme = $seasons_themes[0];
} elseif ( $m > 2 && $m < 6) {
$theme = $seasons_themes[1];
} elseif ( $m > 5 && $m < 9) {
$theme = $seasons_themes[2];
} else {
$theme = $seasons_themes[3];
}
$root = get_theme_roots();
$path = false;
if ( is_string($root) ) {
$path = WP_CONTENT_DIR . \'/\' . $root . \'/\' . $theme ;
} elseif ( is_array($root) && isset($root[$theme]) ) {
$path = WP_CONTENT_DIR . \'/\' . $root[$theme] . \'/\' . $theme;
}
// if the theme exist return its name, if not return false
// (theme setted in backend will be used)
if ( $path && file_exists($path) ) return $theme;
return false;
});