1

I am wondering how to conditionally load child themes? I have a site that is going to change with the seasons. The underlying architecture will remain the same, just different styles and a few minor changes. It seems that child themes is my best bet for this, vs using a bunch of conditional statements within my main theme and loading different stylesheets and what not.

What I am stuck on is how to properly load a specific child theme based on a date?

4

Wordpress use an option to store the current active child theme. This option is named 'stylesheet'.

Using the filter 'pre_option_{$option_name}' you can change every option getted by WP.

So, in your case you should hook the 'pre_option_stylesheet' filter, use some logic to retrieve current season and return the child theme you want. This workflow should be done in a plugin (loaded before theme and child theme), in this way you are sure it works.

Example plugin code:

<?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;  
});
  • Wow, thank you. That is exactly what I am looking for. – Haberg Sep 17 '13 at 23:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.