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.
Then you will have to update
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);