Function to activate WordPress theme inside a plugin

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.

Leave a Comment