Switching theme only changes style sheet being used

I have a plugin that I’m using to switch themes for a specific user for development. When logged in as that user they see the different style sheet but the themes files, function.php, header.php, etc are the active theme for everyone else.

What I am doing wrong?

function change_user_theme($template) {
    global $user_ID;

    if ( in_array( $user_ID, array( 5 ) ) ) {
        $template = 'fezmobile';
    } else {
        $template = 'fezforprez';
    }

    return $template;
}

add_filter('template', 'change_user_theme');
add_filter('stylesheet', 'change_user_theme');
add_filter('option_template', 'change_user_theme');
add_filter('option_stylesheet', 'change_user_theme');

Related posts

Leave a Reply

2 comments

  1. Take a look at my answer here:

    Switch Theme Through Options Panel

    The code is as follows:

    add_action( 'setup_theme', 'switch_user_theme' );
    function switch_user_theme() {
        if ( in_array( wp_get_current_user()->ID, array( 5 ) ) ) {
             $user_theme = 'fezforprez';
             add_filter( 'template', create_function( '$t', 'return "' . $user_theme . '";' ) );
             add_filter( 'stylesheet', create_function( '$s', 'return "' . $user_theme . '";' ) );
        }
    }
    

    You have to swap the template and stylesheet on the setup_theme action.

  2. I actually found a better solution, but only after marking the answer above, as the answer.

    <?php add_action('setup_theme', 'switch_user_theme');
    
    function switch_user_theme() {
        if ( ! is_admin() && current_user_can( 'administrator' ) ) {
             $user_theme = 'name-of-your-theme-directory';
             add_filter( 'template', create_function( '$t', 'return "' . $user_theme . '";' ) );
             add_filter( 'stylesheet', create_function( '$s', 'return "' . $user_theme . '";' ) );
        }
    } ?>
    

    Using this code instead of the one supplied by @sanchothefat, functions like:

    <?php echo get_template_directory_uri(); ?>
    

    Will now work correctly.