
In WordPress, changing themes are easy. But what when you have to change a theme programmatically? Just read the following recipe to find out. It’s a lot easier than you may think!

In WordPress, changing themes are easy. But what when you have to change a theme programmatically? Just read the following recipe to find out. It’s a lot easier than you may think!
The first thing you have to do is to paste the following function in your functions.php file.
function switchTheme($theme) {
global $wpdb;
if (isset($theme)) {
$queries = array("UPDATE wp_options SET option_value = 'default' WHERE option_name = 'template';", "UPDATE wp_options SET option_value = 'default' WHERE option_name = 'stylesheet';", "UPDATE wp_options SET option_value = 'default' WHERE option_name = 'current_theme';");
foreach ($queries as $query){
$wpdb->query($query);
}
}
}
What I've done in the function was simply to update the wp_options table (change the prefix if necessary) with a new theme name. You probably noticied that I used queries in a loop, which isn't a good practice. There's for sure a better way to do it but since I'm not a SQL expert I can't get anything better. If you know how to achieve the same effect without using looped queries, don't hesitate to leave me a comment!
Once you've pasted the function in your functions.php file, you can call it, for example using a filter. The $theme parameter is the theme name. For example default to restore the good old Kubrick theme.
11 Responses
How about using
update_option( $option_name, $newvalue )?Then you could create an array with the option name as $key and the new value as $value.
Upside: You don’t have to be a SQL expert, since WordPress is doing it for you
Should you set the value to ‘$theme’ and not ‘default’?
Anyway, this Query should work better. It updates all 3 rows in one query and gets the table prefix:
function switchTheme($theme) {
global $wpdb;
if (!empty($theme)) {
$wpdb->prefix
$queries = “UPDATE “.$wpdb->prefix.”options SET option_value = ‘”.$theme.”‘ WHERE option_name = ‘template’ OR option_name = ‘stylesheet’ OR option_name = ‘current_theme’;”;
$wpdb->query($query);
}
}
I didn’t test it, but I’m pretty sure it works.
I’ve just wanted to comment how to combine 3 queries into 1 using “OR” statement. But Jacob has done it! And he also consider to use the $theme variable, which the author forgot. Nice work.
@Jacob : Thanks!
simple and easy, thank you for wp trick.
simple and easy, need to check whether any more advanced trick works!!
Great Recipe! I was actually searching on a similar topic last night but resorted to using a plugin instead. But at least now I know.
Or you can use switch_theme($template, $stylesheet)
But what happens if persistant caching is in use?
It stores all options, and if you update database directly, cache won’t know it was changed and will become inconsistent.
You also used wp_options instead of $wpdb->options, that won’t work for security-aware sites!
For editing options, I highly advise using update_options(), it handles everything. Never hardcode dynamic data or do direct database access when you have functions to do it.
Trackbacks: