Change your WordPress theme programmatically.

by Rabbit

First, WordPress is the devil. And all those who spend time developing WordPress, or developing for WordPress, are slaves to his cock. Yes, that means I, too, must suckle the beast’s meat from time to time. However, I am determined to make my suffering worth something. I will share my experiences with you, in the hopes that doing so will minimize the amount of time you spend with your lips wrapped around that totem pole of evil. That said…

If you want to programmatically change your WordPress theme (ONLY tested with WP 2.8), there are two sets of steps. The first set includes gathering data — no actual code is written, but you should write down two important values. Here are the first 4 steps:

Step 1
Identify the theme you wish to use. Do this even if you want to let your end-user decide between multiple themes. This step is required for a conceptual understanding of what you are doing.

Step 2
Locate the theme under wp-content/themes/. Note the theme’s directory name (e.g. “classic”, “default”, “skinbu”, etc.). You will use this value later.

Step 3
Within the theme’s directory will be a style sheet, most probably named “style.css”. Open it.

Step 4
Inside the style sheet, towards the top, you should see a line similar to “Theme Name: WordPress Default”. Note the value in place of “WordPress Default”. You will use this value later.

Intermission
The next set of steps involves some database action, along with any other additional programming you may want for your particular application of this idea. The basic flow is to update two fields with the value of the theme’s directory, delete the existing current_theme option, then recreate it with the “Theme Name” value inside the theme’s style sheet. Here we go.

Step 5
Issue a query similar to the following:

UPDATE `wp_options` SET `option_value` = 'value from step 2' WHERE `option_name` = 'template'

Replace “value from step 2″ with the value you obtained from step  2 above.

Step 6
Issue a query similar to the following:

UPDATE `wp_options` SET `option_value` = 'value from step 2' WHERE `option_name` = 'stylesheet'

Replace “value from step 2″ with the value you obtained from step 2 above.

Step 7
Issue a query similar to the following:

DELETE FROM wp_options WHERE option_name = 'current_theme'

Step 8
Issue a query similar to the following:

INSERT INTO `wp_options` (`option_name`,`option_value`,`autoload`) VALUES ('current_theme','value from step 4','yes')

Replace “value from step 4″ with the value you obtained in step 4 above.

Done.