Dec.02

Switch WordPress Theme by User

I’m getting tired of my current layout on this site, so I’m going to redesign it. Since a redesign is only the aesthetics of a website, there should be no reason to re-install a whole new WordPress installation. Instead we are going to use the same data, just change the theme, based on the current user that is logged in.

In your current templates functions.php file add this :

function development() {
global $devUsers;
$devUsers = array('dev'=>'dev');

function user_theme_template($template = '') {
global $devUsers;
$current_user = wp_get_current_user();

if (in_array($current_user->user_login, $devUsers )) {
return $devUsers[$current_user->user_login];
}
return $template;
}

function user_theme_stylesheet($stylesheet = '') {
global $devUsers;
$current_user = wp_get_current_user();
if (in_array($current_user->user_login, $devUsers )) {
return $devUsers[$current_user->user_login];
}
return $stylesheet;
}

add_filter('template',   'user_theme_template',100);
add_filter('stylesheet', 'user_theme_stylesheet',100);
}

add_action("init","development");

This function checks the current username against an array of allowable usernames to see the developing themes. In our case, we are looking for the user ‘dev’, and the user will be given the theme ‘dev’ instead of the normal theme.

Programming,WordPress

Leave a comment