1

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');
2

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.

| improve this answer | |
0

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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