Switch Theme Through Options Panel

If your theme has the theme-options built in then if people were to switch it the admin aspect of the theme would be lost if it wasn’t in every theme so what I would do is to either:

  • Make the available themes for switching to into child themes of the one that has all the backend functionality.

You’d have to edit any themes you get to be child themes and do that every time you update them. Not much fun if these are themes you’re grabbing from the repository or a 3rd party.

or

  • Make the theme-options page functionality into a plugin.

Regardless of the theme it will be available and you can just drop in any other themes you like without having to edit them.

In terms of code there are 2 filters you can use where the theme routing stems from. For child themes you only need to filter the stylesheet:

add_action( 'setup_theme', 'switch_user_theme' );
function switch_user_theme() {
    if ( false != ( $user_theme = get_user_meta( wp_get_current_user()->ID, 'theme', true ) ) ) {
         add_filter( 'template', create_function( '$t', 'return "' . $user_theme . '";' ) );
         add_filter( 'stylesheet', create_function( '$s', 'return "' . $user_theme . '";' ) );
    }
}

NB. using the switch_theme() function updates the default theme for the whole site, not just the user.

Leave a Comment