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

Show widget based on User Role or Role Scoper User Group WordPress

I have been working on a private LMS for a small school. I am using Role Scoper for showing content based on student user groups. I use the Members plugin to add extra roles. I use a registered only plugin to make the site private.
Issue is that the StudioPress Enterprise theme I am using, uses lots of need widgets (Genesis and WP widgets) to load content on home. I want to show different widgets per user group or user role. Preferably by Role Scoper user group. So I installed Widget logic hoping to be able to pull this of with a simple if statement. Here is three code snippets I tried:

<strong>Filter by WordPress Role:</strong>

if ( current_user_can( 'role' ) )

<strong>Role Scoper's code snippets to filter by user group:</strong>

// substitute actual group ID here (id argument in edit links on User > Role Groups)
$group_id_in_question = 9;

global $current_user;
if ( isset( $current_user->groups[ $group_id_in_question ] ) ) {
echo "hhh";
}


$group_id_in_question = 9; // my RS User Group
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
if ( isset( $current_id->$group_id[ $group_id_in_question ] ) ) echo "www";
else
echo "hhh";


None work, all return nothing or the else statement. I think it is an Widget Logic Issue as an if statement filerting by role did work inside a PHP Widget. But a PHP widget does not allow me to show or hide a certain widget completely based on WP Role or RS User Group. What to do?

NB See WP Support threads here:
[[LINK href="http://wordpress.org/support/topic/role-scoper-conditional-data-display-functions?replies=8"]]role-scoper-conditional-data-display-functions[[/LINK]]
[[LINK href="http://wordpress.org/support/topic/plugin-members-using-widget-logic-to-hide-widget-by-role?replies=1"]]plugin-members-using-widget-logic-to-hide-widget-by-role[[/LINK]]

Answers (1)

2011-08-16

Kannan C answers:

I suspect that using 3 plugins to control by role may prevent the output. Have you tried having only the Role scoper?
FYI: Placing the below code in the header.php can do what the "Registered only" plugin

global $user_ID, $user_identity, $user_level;
if (!$user_ID) exit('This is protected');


rhand comments:

Well I disabled Members plugin as I theoretically should be able to filter by RS User Group. Also removed the extra added role.
Role Scoper has currently user group ID 9 and ID 10 and each user group has one subscriber added to it. <em>What RS related code can I add to the widget logic to make a widget show for group ID 9 or 10?</em>

NB Checking out your Registered only Code, but I might loose some Registered Only options so not sure yet.


Kannan C comments:

I am not sure about is there any predefined function in role scoper. But by digging its table struc, i came to this


function get_user_gp($userid) {
global $wpdb;
$custom_query = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->user2group_rs WHERE user_id = '".$userid."'") );
return $custom_query->group_id;
}
echo get_user_gp(1);

This will return the group id of a logged in user. you can do something like

if(get_user_gp(1) == 12) {
//do stuff
}

let me know, whether this works.


rhand comments:

Will try this later today. In transit now. I guess I could add this function to functions.php of my child theme, right? And then add the if condition to the widget logic code box?


Kannan C comments:

Well, if you want to use call this function from widget logic, you need some modification. I did a quick edit to my function. Paste this new function in the functions.php (not the previous one)

function is_usergroup_can($groupid) {
global $wpdb, $user_ID, $user_identity;
$custom_query = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->user2group_rs WHERE user_id = '".$user_ID."'") );
if($custom_query->group_id > 0)
return 1;
}

and call from widget logic like

is_usergroup_can(9)


rhand comments:

Works, but a user under Role Scoper user group 9 or 10 can see widgets for both groups. I wonder if the is an issue with the function or with the Role Scoper Configuration


rhand comments:

is_usergroup_can(9) && !is_usergroup_can(10) and is_usergroup_can(10) && !is_usergroup_can(9) will work. That might because the
if($custom_query->group_id > 0)
return 1;
will show a widget width an ID larger than zero. Not sure though. But I need to exclude another user group to make that one return 0. Perhaps an isset is better for the if statement. Will test some more in the morning.


Kannan C comments:

Oh sorry. i used > to check if the function works and i forgot to change it. You actually need to use equal logic there. just change the line

if($custom_query->group_id > 0)

to
if($custom_query->group_id == $groupid)
isset and if will do the same result here.


rhand comments:

Thanks, works. Closed the question. Still going through your function to get a better understanding of the current if statement. == means if it has a $group_id I guess.