0

I am trying to switch my current custom theme to twenty fifteen theme if the visitor is admin. So I put following code into my custom theme functions.php

/*** Switching theme to Admin ***/
add_action( 'setup_theme', 'switch_user_theme' );
function switch_user_theme() {
    if ( current_user_can( 'manage_options' ) ) {
         $user_theme = 'Twenty Fifteen';
         add_filter( 'template', create_function( '$t', 'return "' . $user_theme . '";' ) );
         add_filter( 'stylesheet', create_function( '$s', 'return "' . $user_theme . '";' ) );
    }
}

But when an admin visit to the site, still it shows custom theme not the twenty fifteen Why it is not switched to twenty fifteen theme when an admin visit to the site?

0

2 Answers 2

2

You are sort of doing this in a roundabout way. WordPress has a function called switch_theme():

add_action( 'setup_theme', 'switch_user_theme' );
function switch_user_theme() {
  if ( current_user_can( 'manage_options' ) ) {
    switch_theme('twentytwelve');
  } else {
    switch_theme('twentythirteen');
  }
}

The argument is the directory of the theme you want.

I can't help but think this is a bad idea though. Surely you can do what you need without switching themes constantly?

2
  • I tend to agree with you here, a better solution would be to load different stylesheets per user conditionally. I'm thinking (though not sure, haven't tested it) that if you switch themes in the manner you have done, this will affect all users. Jun 3, 2015 at 5:54
  • 1
    "...this will affect all users..." -- it will, hence the switch back on the else condition. This works, and it answers the question, but it isn't a good idea ;)
    – s_ha_dum
    Jun 3, 2015 at 5:57
1

The current WordPress theme name is saved in the wp_options table of your WordPress database. The easy way to do it is to use the update_option() function, as shown in the function below. Paste it in your functions.php file.

function updateTheme($theme){
        update_option('template', $theme);
        update_option('stylesheet', $theme);
        update_option('current_theme', $theme);
}

Call to the function can be made the following way into your custom theme functions.php:

add_action( 'setup_theme', 'switch_user_theme' );
function switch_user_theme() {
        if ( current_user_can( 'manage_options' ) ) {
                $theme = "twentyfifteen";
        }
        else {
                $theme = "default";
        }
        updateTheme($theme);
}

Hope this helps you out easily.

Thanks!

1
  • 1
    Rather than changing the theme option back & forth constantly, what about using the option_* filters (option_template, option_stylesheet, and option_current_theme in this case) to override their values when an admin is viewing the site? The effect will be the same, without extra writes to the database and with fewer possible side-effects.
    – Dave Ross
    Jun 3, 2015 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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