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;
}
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;
}
Share
Improve this question
edited Apr 13, 2017 at 12:37
CommunityBot
1
asked Dec 30, 2016 at 4:16
RanukaRanuka
1,7942 gold badges18 silver badges31 bronze badges
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 – Mark Kaplun Commented Dec 30, 2016 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. – Mayeenul Islam Commented Dec 30, 2016 at 5:00
1 Answer
Reset to default 3At 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;
}