Post Pic

WordPress trick: Change theme programatically

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.

12 Responses

Feb 22 2010 09:07

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 :)

Feb 22 2010 09:10

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.

Feb 22 2010 14:00

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.

Feb 22 2010 15:26

@Jacob : Thanks!

Feb 23 2010 02:22

simple and easy, thank you for wp trick. :)

Mar 06 2010 11:02

simple and easy, need to check whether any more advanced trick works!!

Mar 10 2010 17:56

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. :)

Jun 04 2010 16:24

Or you can use switch_theme($template, $stylesheet)

Jun 25 2010 04:17

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.

Aug 25 2010 10:56

@Jacob:
Thanks.

@Jean-Baptiste Jung:
Useful if you have a website for iPhone with a custom theme, but don’t want to duplicate the content.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required