Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Switch theme based upon if_user_can, need fix for WP 3.1 WordPress

  • REFUNDED

WPEngineer [[LINK href="http://wpengineer.com/793/change-your-wordpress-theme-on/"]]wrote a function[[/LINK]] two years ago to switch themes based upon a number of variables. As of 3.1 this code no longer works, it breaks the theme display. I need this script updated to work with the latest version.

The public theme name should be inserted via an Options Framework variable while the if_user_can answer remains constant.

So for example:
$themedir = of_get_option('optf_themedir', 'twentyten' );

function fb_user_theme( $template = '' ) {
if ( current_user_can('edit_options') ) {
$template = 'onlyiol';
}
else {
$template = $themedir;
}
return $template;

}
add_filter('template', 'fb_user_theme');
add_filter('stylesheet', 'fb_user_theme');

Answers (2)

2011-05-17

Utkarsh Kukreti answers:

You need to move the $themedir assignment inside the function.

function fb_user_theme( $template = '' ) {
$themedir = of_get_option('optf_themedir', 'twentyten' );
if ( current_user_can('edit_options') ) {
$template = 'onlyiol';
}
else {
$template = $themedir;
}
return $template;
}

add_filter('template', 'fb_user_theme');
add_filter('stylesheet', 'fb_user_theme');


Matt Taylor comments:

I tried it in and out and as a hard-coded variable but the theme is still breaking.

With your snippet the active theme (as set in the Appearance tab) is being displayed to users with permission and visitors without the permission only see a blank page.

I attached a screenshot of what the has-permission user is seeing