Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a theme. It depends on a plugin. If you try to activate the theme and the plugin isn't installed, then I want to switch_theme() to some other (random**) theme in the search_theme_directories() list.

I can get the $template value from the key of the array returned by search_theme_directories(). But I can't seem to sort out how to get the $stylesheet.

Also, given this approach, what's the best way to check if a theme is child or a parent? Without getting into details I would prefer to pick parent theme as a default.

Perhaps there's a better way to do this? I would presume WP has something similar built in but I can seem to pin point that either. In short, I'm listenin' if you got ideas :)

Important

I was not able to tag this with: search_theme_directories(). That tag doesn't exist and I don't have the rep to add it. Could someone with some juice add it to the list of valid tags? Thanks.

Note: I'm first going to check for twentytwelve and twentyeleven and then if necessary pick one if they aren't around.

share|improve this question

1 Answer 1

But I can't seem to sort out how to get the $stylesheet.

Easy.

$themes = search_theme_directories();

// a random theme
$themename = array_rand($themes,1); 

// that random theme's stylesheet
$stylesheet = content_url('/themes/'.$themes[$themename]['theme_file']);

// The absolute path to that theme's stylesheet
$stylepath = WP_CONTENT_DIR.'/themes/'.$themes[$themename]['theme_file'];

Getting a random theme that is not a child is more involved. I think this is it, though. It works in testing but don't consider it rigorously tested.

$themes = search_theme_directories();
$rand_theme = false;
while (!$rand_theme) {
  if (empty($themes)) {
    break;
  }
  $themename = array_rand($themes,1);
  $theme_obj = wp_get_theme($themename); 
  // Parent themes don't have a 'Template'
  $template = $theme_obj->get('Template');
  if (empty($template)) {
    $rand_theme = $themename;
    break;
  } else {
    unset($themes[$themename]);
  }
}
switch_theme($rand_theme);

wp_get_theme returns a Theme Object. I set $rand_theme to the theme name since that is what switch_theme expects.

As is, that will switch themes on every page load. Drop that into the plugin directory and you have yourself a pretty annoying plugin :)

share|improve this answer
    
Thx @s_ha_dum but I don't think the first bit is going to work. For example, this is what works to switch to twentytwelve: switch_theme('twentytwelve','twentytwelve'); Notice how "stylesheet" ain't really so. I tried a number of different things, yours included and this is the only thing that work. As for get_the_theme(). If that gives me an object with theme details then that'll do. Cheers! Perhaps you can take another shot at the first bit? –  Chief Alchemist Apr 6 '13 at 23:13
1  
What 'first bit' are you talking about? I dropped that code into a plugin file and it randomly switches themes on every page load as long I as keep reloading. I just re-pasted that second block from that plugin file just in case I had something wrong. –  s_ha_dum Apr 6 '13 at 23:33
    
Oh. Sorry. Specifically, this: $stylesheet = content_url('/themes/'.$themes[$themename]['theme_file']); I tried that and WP didn't like it. switch_theme() doesn't want/need the folder structure. it wants a name. e.g. switch_theme('twentytwelve','twentytwelve'); codex.wordpress.org/Function_Reference/switch_theme. This is what had be confused and in search of a viable solution. Thanks again for your help. –  Chief Alchemist Apr 7 '13 at 12:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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