Switch Active WordPress Theme For Specific User


About The Author
Cabe Nolan is the founder of WP Cover where he shares his insight into WordPress, development, & entrepreneurship. Outside of WPCover, Cabe continues to run a successful WordPress development firm, Bold City Design as well as a few high profile websites, Arrivala, Two Way Resume, Dock Skipper, and a successful outdoors brand, DolfinPack.







Switch Active WordPress Theme For Specific User

When designing or developing a new feature for a WordPress site it is often helpful to duplicate the theme, make changes in a test theme and then push the changes back to the live theme.  People have developed some sophisticated plugins that allow you to specify which user roles see what theme but in my opinion most of them are over the top for what I typically need.  I don’t want to spend time configuring a plugin and writing unnecessary data to the database when I’m probably going to uninstall the plugin a couple days later.

If you’re like me, you may be looking for a clean piece of code to quickly switch a specific user or users to view an alternative theme.  I have that clean piece of code for you, short and simple.  Simply create a file in your plugins directory and call it whatever you want (maybe theme-switch.php).  Paste in the following code and edit line 11 with your user ID and line 12 with the name of your theme directory.  Save the file, activate the plugin and you’re on your way!


<?php

/*
Plugin Name: User Theme Switch
*/

add_filter('template', 'change_theme');
add_filter('option_template', 'change_theme');
add_filter('option_stylesheet', 'change_theme');
function change_theme($theme) {
    if ( get_current_user_id() == 21) {
        $theme = 'enterthemename';
    }

    return $theme;
}

To make it even easier, here is a downloadable version of the above code. You’ll still want to swap out lines 11 and 12 with your appropriate information.


Download File

Enjoy!


Share Your Thoughts

Leave a Reply

Your email address will not be published.


Related Stuff You Might Like







WordPress News, Tips, & Code Snippets

Join the WP Cover mailing list and get wordpress news, tips, code snippets, security warnings, and more delivered right to your inbox.  We won't flood your inbox, newsletters typically go out every 1-2 weeks unless it involves an important security release.

You have Successfully Subscribed!