3

I am creating a plugin that generates a theme, and so I want to have a checkbox at the end of the theme generation process that gives the possibility to activate the freshly created theme without having to do it manually. Is there any function that can do that?

2 Answers 2

3

Of course there’s a function for that (Codex):

switch_theme( $stylesheet )

It:

Switches current theme to new template and stylesheet names. Accepts one argument: $stylesheet of the theme. ($stylesheet is the name of your folder slug. It's the same value that you'd use for a child theme, something like twentythirteen.) It also accepts an additional function signature of two arguments: $template then $stylesheet. This is for backwards compatibility.

And why is that any better? WordPress uses filters and actions for many things. For example, when you switch the theme, the unused widgets will get saved, so you can restore them in new sidebars... All of that won’t be done, if you switch the theme directly in DB.

1

The only thing that comes to my mind is to do it from the database, so basically after you check the checkbox status, you will have to select the wp_options table, you will need to locate two rows template and stylesheet.

enter image description here

Then you will have to update alexandria for youThemeName. That query will look like this:

$query = "UPDATE wp_options SET option_value='youThemeName' WHERE option_name='template' OR option_name='stylesheet';";
$wpdb->query($query);
2
  • I don’t think that’s the best way to do it. It’s always pretty risky to mess with DB in such cases... Commented Jul 12, 2018 at 7:24
  • Thanks for pointing that out, I didn't know about that. Commented Jul 12, 2018 at 11:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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