Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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');
share|improve this question

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer
    
Thanks for this, it works perfectly. –  kel Apr 24 '12 at 16:29

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.