最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Activate a new WordPress Theme Only for Admins

programmeradmin5浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 3

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;
}
发布评论

评论列表(0)

  1. 暂无评论