Activate a new WordPress Theme Only for Admins

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;
    }

1 Answer
1

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 environment. 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, their 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;
}

Leave a Comment