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

Change theme based upon user role level WordPress

  • SOLVED

I'm are working on a project for a client that involves showing one theme to employees (user level 2 or higher) and another to people not logged in or user level 1. WPEnginer has a snippet that should work however I'm missing something obvious in the code.

For the sake of clarity we will identify the themes as follows:
user level 2 or higher: redrover
user level 1, not logged in, or category "iol" : greengoblin

Here is the complete code.
function fb_user_theme($template = '') {
global $user_ID;

// en_US: replace space in the name of a theme-folder with _ (underline)!
// de_DE: Leerzeichen im Namen des Themes muessen mit _ (underline) ersetzt werden!

// when profil-ID
if ( in_array( $user_ID, array( 1, 21 ) ) ) {
$template = 'redrobin-dev';
}

// when IP
elseif ( in_array( $_SERVER['REMOTE_ADDR'], array('127.0.0.1', '127.0.0.2') ) ) {
$template = 'redrobin';
}

// when User has capabilities (example: Administrator -> manage_options)
// @link see http://codex.wordpress.org/Roles_and_Capabilities
elseif ( current_user_can('manage_options') ) {
$template = 'redrover';
} elseif ( current_user_can('edit_posts') ) {
$template = 'redrover';
} elseif ( current_user_can('read') ) {
$template = 'greengoblin';
}

// when category with ID
// @link http://codex.wordpress.org/Template_Tags/in_category
// elseif ( in_category( array(1, 'iol', 'IOL') ) ) {
// $template = 'greengoblin';
// } else {
// $template = 'redrover';
// }


// when User_Level
// @see http://codex.wordpress.org/Roles_and_Capabilities#Roles
if ( current_user_can('level_10') ) {
$template = 'redrover';
}
elseif ( current_user_can('level_1') ) {
$template = 'greengoblin';
}
elseif ( current_user_can('level_0') ) {
$template = 'greengoblin';
}

return $template;
}

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

The code can be found here: [[LINK href="http://wpengineer.com/793/change-your-wordpress-theme-on/"]]http://wpengineer.com/793/change-your-wordpress-theme-on/[[/LINK]]

The winning bid will be the first to rewrite the code to function properly.

Answers (6)

2011-01-22

Oleg Butuzov answers:

function fb_user_theme($template = '') {

global $user_ID;

if (!is_user_logged_in()) {
$template = 'greengoblin';
return $template;
}

// en_US: replace space in the name of a theme-folder with _ (underline)!

// de_DE: Leerzeichen im Namen des Themes muessen mit _ (underline) ersetzt werden!



// when profil-ID

if ( in_array( $user_ID, array( 1, 21 ) ) ) {

$template = 'redrobin-dev';

} elseif ( in_array( $_SERVER['REMOTE_ADDR'], array('127.0.0.1', '127.0.0.2') ) ) {

// when IP
$template = 'redrobin';

} elseif ( current_user_can('manage_options') || current_user_can('edit_posts')) {
// when User has capabilities (example: Administrator -> manage_options)
// @link see http://codex.wordpress.org/Roles_and_Capabilities

$template = 'redrover';

} elseif ( current_user_can('read') ) {

$template = 'greengoblin';

}


// when User_Level
// @see http://codex.wordpress.org/Roles_and_Capabilities#Roles

if ( current_user_can('level_10') ) {

$template = 'redrover';

} elseif ( current_user_can('level_1') ) {

$template = 'greengoblin';

} elseif ( current_user_can('level_0') ) {

$template = 'greengoblin';

}

return $template;
}

2011-01-21

Utkarsh Kukreti answers:

What exactly is not working?

Try changing

if ( current_user_can('level_10') ) {


to

elseif ( current_user_can('level_10') ) {


Utkarsh Kukreti comments:

Also, you should add

get_currentuserinfo();

after

global $user_ID;


Matt Taylor comments:

Utkarsh,

Due to a very tight deadline I need a drop and replace solution. Please post your code in a modified version of the original snippet.

Thanks!


Utkarsh Kukreti comments:

function fb_user_theme($template = '') {
global $user_ID;
get_currentuserinfo();

// en_US: replace space in the name of a theme-folder with _ (underline)!
// de_DE: Leerzeichen im Namen des Themes muessen mit _ (underline) ersetzt werden!

// when profil-ID
if ( in_array( $user_ID, array( 1, 21 ) ) ) {
$template = 'redrobin-dev';
}

// when IP
elseif ( in_array( $_SERVER['REMOTE_ADDR'], array('127.0.0.1', '127.0.0.2') ) ) {
$template = 'redrobin';
}

// when User has capabilities (example: Administrator -> manage_options)
// @link see http://codex.wordpress.org/Roles_and_Capabilities
elseif ( current_user_can('manage_options') ) {
$template = 'redrover';
} elseif ( current_user_can('edit_posts') ) {
$template = 'redrover';
} elseif ( current_user_can('read') ) {
$template = 'greengoblin';
}

// when category with ID
// @link http://codex.wordpress.org/Template_Tags/in_category
// elseif ( in_category( array(1, 'iol', 'IOL') ) ) {
// $template = 'greengoblin';
// } else {
// $template = 'redrover';
// }


// when User_Level
// @see http://codex.wordpress.org/Roles_and_Capabilities#Roles
elseif ( current_user_can('level_10') ) {
$template = 'redrover';
}
elseif ( current_user_can('level_1') ) {
$template = 'greengoblin';
}
elseif ( current_user_can('level_0') ) {
$template = 'greengoblin';
}

return $template;
}

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

2011-01-21

Jens Filipsson answers:

Is there a reason why you have commented out the following lines? ( // )

// elseif ( in_category( array(1, 'iol', 'IOL') ) ) {

// $template = 'greengoblin';

// } else {

// $template = 'redrover';

// }


Maybe you need that else statement?


Matt Taylor comments:

There was no difference with them being commented out, deleted, or in place. :)

2011-01-21

Ivaylo Draganov answers:

I think you should use capabilities instead of user levels. User levels are deprecated in favour of roles and capabilities. So, try this:

<?php
function fb_user_theme($template = '') {

global $user_ID;

// en_US: replace space in the name of a theme-folder with _ (underline)!
// de_DE: Leerzeichen im Namen des Themes muessen mit _ (underline) ersetzt werden!

// when profil-ID
if ( in_array( $user_ID, array( 1, 21 ) ) ) {
$template = 'redrobin-dev';
}

// when IP
elseif ( in_array( $_SERVER['REMOTE_ADDR'], array('127.0.0.1', '127.0.0.2') ) ) {
$template = 'redrobin';
}

// @see http://codex.wordpress.org/Roles_and_Capabilities#Roles
// administrator
elseif ( current_user_can('activate_plugins') ) {
$template = 'redrover';
}
// editor, author
elseif ( current_user_can('edit_published_posts') ) {
$template = 'redrover';
}
// contributor, subscriber, not logged in
elseif ( current_user_can('edit_published_posts') || !is_user_logged_in() ) {
$template = 'greengoblin';
}

// when category with ID
// @link http://codex.wordpress.org/Template_Tags/in_category

// elseif ( in_category( array(1, 'iol', 'IOL') ) ) {

// $template = 'greengoblin';

// } else {

// $template = 'redrover';

// }


return $template;
}



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


Matt Taylor comments:

This is a special use project that involves 12 custom user roles. Multiple roles share edit privileges so user levels are the best common denominator. Generally though of course you are right.


Ivaylo Draganov comments:

You can combine the conditionals to match a given role then. Something like:

elseif ( current_user_can('activate_plugins') and (current_user_can('edit_posts') ) {

According to the WP Codex <em>"User Levels were finaly deprecated in version 3.0. "</em>, so you really shouldn't count on them. And in their nature they are just capabilities like all the rest.

Another solution would be to create you own capabilities and map them to roles that you want group.

2011-01-22

Sébastien | French WordpressDesigner answers:

EDIT :

you write <blockquote>
user level 2 or higher: redrover
user level 1, not logged in, or category "iol" : greengoblin
</blockquote>
So but if i am not logged in but in category iol what is the template ?


Sébastien | French WordpressDesigner comments:

In this code, if the use is not logged in or
if the user is in category iol
or if the user have level 1
the template is greengoblin
else
the template is redrover
simply :
function fb_user_theme($template = '') {

global $user_ID;

get_currentuserinfo();



if ((!is_user_logged_in()) OR (in_category( array(1, 'iol', 'IOL') ))) {$template = 'greengoblin';}
elseif (is_user_logged_in()) {
if ((current_user_can('level_1')) ) {
$template = 'greengoblin';
}
else {$template = 'redrover';}
}







return $template;

}

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


IMPORTANT : you must paste this code in the file functions.php of the template greengoblin AND in the file functions.php of the template redrover


Sébastien | French WordpressDesigner comments:

Sorry the is a little mistake in the code above
the right code is

function fb_user_theme($template = '') {



global $user_ID;

get_currentuserinfo();

if (!is_user_logged_in()) {$template = 'greengoblin';}
elseif (is_user_logged_in()) {
if (!current_user_can('level_2')) {// current_user_can(level_0) is true OR current_user_can(level_1) is true
$template = 'greengoblin';
}
else {$template = 'redrover';}
}

return $template;

}



add_filter('template', 'fb_user_theme');

add_filter('stylesheet', 'fb_user_theme');



2011-01-24

Pali Madra answers:

Try this plugin at [[LINK href="http://wordpress.org/extend/plugins/adminimize/"]][[/LINK]]- Does it help