1

I am going to change the theme of one of my site. I need to do lots of changes also for new theme.

So what I want to do is, I need to activate the new theme only for admins. When any other user visiting the site, it should use the current theme.

I tried following code but it did not work. It break the site.

Source : Show different theme for admin?

/*
Plugin Name: Theme Switch if Admin
Description: Display different theme to user if logged in as admin
Author: Kyle Barber
*/
    add_filter('template', 'change_theme');
    add_filter('option_template', 'change_theme');
    add_filter('option_stylesheet', 'change_theme');
    function change_theme($theme) {
        if ( current_user_can('manage_options') ) {
            $theme = 'twentyeleven';
        }

        return $theme;
    }
2
  • 1
    Having two themes "running" on the same site is a source for trouble as configuration changes in one, might impact the other. Unless you planned carefully to be able to do it you probably should not do it. Set proper development site for testing and developing your new theme, never make untested code changes on a live site Dec 30 '16 at 4:43
  • What @MarkKaplun said is what exactly you should follow. BTW, when you are ready to kick off, but before, you need to see how thing should be on a live site, plugin like Theme Test Drive could help you. Dec 30 '16 at 5:00
0

At first, you should active the WordPress Debug mode to get the error after implement your code. The code should work, also tested on my side. I use it on a client installation and works really well. See my source below. It is important that you use the right string for the theme slug, like here popper. You should also use this code as a plugin in the installation, not inside a theme. Also the hint, if your installation is a multisite - the theme must be usable for each site, there use the small plugin to switch the theme.

add_filter( 'template', 'fb_change_theme' );
add_filter( 'option_template', 'fb_change_theme' );
add_filter( 'option_stylesheet', 'fb_change_theme' );
add_filter( 'pre_option_stylesheet', 'fb_change_theme' );
function fb_change_theme($theme) {

    if ( current_user_can( 'manage_options' ) ) {
        $theme = 'popper';
    }

    return $theme;
}

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.