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?


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

switch_theme( $stylesheet )


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.


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';";
  • I don’t think that’s the best way to do it. It’s always pretty risky to mess with DB in such cases... Jul 12 '18 at 7:24
  • Thanks for pointing that out, I didn't know about that. Jul 12 '18 at 11:19

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.