0

What is the argument to pass to switch theme, when I have the result of wp_get_themes()?

 $args = array(
        'allowed' => true
 );

 $themes = wp_get_themes(  );

 foreach ($themes as $theme) {

          $argument_to_pass_to_switch_theme = ??????

 }
  • Why are you switching themes in a Loop? What is the use case for this? – s_ha_dum May 4 '14 at 15:43
  • I'm not, I just need the variable to echo it later... – user1049961 May 4 '14 at 15:58
  • For temporary preview/theme switch (while visitor still see the default theme), then you can use plugins: wordpress.stackexchange.com/a/161196/42702 – T.Todua Sep 13 '14 at 8:46
1

If I understand your question, you should use this inside your foreach:

$argument_to_pass_to_switch_theme = $theme->get_stylesheet();

Just taking a look at the documentation:

The function wp_get_themes()...

"...returns an array of WP_Theme objects based on the argument."

This class WP_Theme has a method get_stylesheet(), which...

"...returns a string with the directory name of the theme's "stylesheet" files, inside the theme root. If this is a child theme it is the directory from the child theme. "

And the function switch_theme()...

"...accepts one argument: $stylesheet of the theme. ($stylesheet is the name of your folder slug."

1

wp_get_themes() returns a WP_Theme object, one of the methods of which, get_stylesheet() will return the value you need to pass to switch_theme()

You are not likely to get "the variable" to echo later. If you have more than one theme and is nearly always the case (as you should keep a bundled theme just in case) you are going to have multiple variables, so you need to create an array and not a string.

$themes = wp_get_themes();
foreach ($themes as $theme) {
  $argument_to_pass_to_switch_theme[] =  $theme->get_stylesheet();
}

var_dump($argument_to_pass_to_switch_theme);

If you run the code as you have it mocked up...

$themes = wp_get_themes();
foreach ($themes as $theme) {
  $argument_to_pass_to_switch_theme =  $theme->get_stylesheet();
}

var_dump($argument_to_pass_to_switch_theme);

... you will overwrite the $argument_to_pass_to_switch_theme variable at every iteration of the loop and only end up with the stylesheet name for the last theme in the loop.

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.