Title: Getting Roles from &#039;Groups&#039; (Groups plugin)
Last modified: August 21, 2016

---

# Getting Roles from 'Groups' (Groups plugin)

 *  [philzo](https://wordpress.org/support/users/philzo/)
 * (@philzo)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/getting-roles-from-groups-groups-plugin/)
 * I’m using [Groups plugin](https://wordpress.org/plugins/groups/) and Nav Menu
   Roles and I thought it would be handy to automatically list group names under‘
   Access Role’ when editing menu items. That way I could check off a group name
   to give that group permission to view a menu item. To make this work, I had to
   do two things:
 * 1) Create a capability in Groups with the same name as the group and assign that
   capability to the group. For example, for my group ‘students’ I created the capability‘
   students’ and assigned it to the students group.
 * 2) Create a variation on the code outlined by helgatheviking [here](http://wordpress.org/support/topic/feature-request-support-for-capabilities?replies=7#post-5413340)
   to pull Group id’s and Group names and set them to custom role keys and role 
   names.
 *     ```
       function roles_to_groups ( $roles ) {
       	global $wpdb;
   
       	$order_by = 'name';
       	$order = 'ASC';
   
       	$group_table = _groups_get_tablename( "group" );
       	if ( $groups = $wpdb->get_results(
       			"SELECT group_id FROM $group_table ORDER BY $order_by $order" ))
       		{
       			foreach( $groups as $group ) {
       				$group = new Groups_Group( $group->group_id );
       				$roles [$group->group_id] = $group->name;
       		}
       	}
   
       	return $roles;
       }
   
       add_filter( 'nav_menu_roles', 'roles_to_groups' );
       ```
   
 * [https://wordpress.org/plugins/nav-menu-roles/](https://wordpress.org/plugins/nav-menu-roles/)

Viewing 9 replies - 1 through 9 (of 9 total)

 *  Plugin Author [HelgaTheViking](https://wordpress.org/support/users/helgatheviking/)
 * (@helgatheviking)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/getting-roles-from-groups-groups-plugin/#post-5085801)
 * Thanks for sharing this!
 * It seems like there ought to be a better way to get a list of the group names
   than a direct query on the database, but I’m not familiar with how Groups works.
 *  Thread Starter [philzo](https://wordpress.org/support/users/philzo/)
 * (@philzo)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/getting-roles-from-groups-groups-plugin/#post-5085900)
 * You’re welcome. Thanks for the great plugin!
 * I agree… there ought to be a better way. I looked for a while and found a few
   posts like this [Groups support thread](http://wordpress.org/support/topic/getting-all-groups-names-through-function?replies=2)
   that point to a direct query solution on [github](https://github.com/eggemplo/Groups-Utilities/blob/master/get_groups.php).
   I ended up borrowing from Groups’ shortcode handler code from the plugin itself(
   see lib/views/class-groups-shortcode.php).
 * If I come up with something better I’ll add to this topic.
 *  Plugin Author [HelgaTheViking](https://wordpress.org/support/users/helgatheviking/)
 * (@helgatheviking)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/getting-roles-from-groups-groups-plugin/#post-5085901)
 * I just took a very cursory look and don’t see anything in the way of a template
   tag either… and because the groups are “posts” I don’t really know how else to
   query them other than what you’ve done. There is an open thread on the Groups
   board asking this same question. Maybe the author will answer that.
 * Anyway, if you don’t want to add caps with the same name you could filter `$visible
   = apply_filters( 'nav_menu_roles_item_visibility', $visible, $item );`. Though,
   I couldn’t tell you how to determine if the current user in in a specific group?
   In that sense, maybe your way is easier after all.
 *  Thread Starter [philzo](https://wordpress.org/support/users/philzo/)
 * (@philzo)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/getting-roles-from-groups-groups-plugin/#post-5085996)
 * Hi, helgatheviking. I’m not sure if this answers your question
 * > I couldn’t tell you how to determine if the current user in in a specific group?
 *  but you can try the following which I also posted in Groups forum [under your comment](http://wordpress.org/support/topic/list-of-groups?replies=2#post-5788186)
   🙂
 *     ```
       $user = new Groups_User( $user_id );
       $groups = $user->groups;
   
       print_r($groups);
       ```
   
 *  Plugin Author [HelgaTheViking](https://wordpress.org/support/users/helgatheviking/)
 * (@helgatheviking)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/getting-roles-from-groups-groups-plugin/#post-5085998)
 * That should help when it comes to filtering the visibility via `'nav_menu_roles_item_visibility'`
   in lieu of creating capabilities with names that match the group name. Probably
   some kind of `in_array()` check.
 *  Thread Starter [philzo](https://wordpress.org/support/users/philzo/)
 * (@philzo)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/getting-roles-from-groups-groups-plugin/#post-5086020)
 * So I’ve been exploring the ‘nav_menu_roles_item_visibility’ as helgatheviking
   suggested and ended up writing a plugin to add Group names as custom roles and
   enable a filter to turn on menu items when a group-based role has been selected.
   This approach reduces chance of user error and the redundancy of having to add
   a capability within Groups.
 * The plugin has two functions:
    1) nmr_groups_to_roles – almost exactly like the
   function that I started this thread with except the name is more accurate and
   the $key for $roles[$key] is also the name of the Group. This is important because
   when we compare arrays of Group names and NMR roles in the next function, $items-
   >roles is an array of the $key values, not the names of the roles. (‘nmr’ is 
   short for Nav Menu Roles, of course.) (BTW – I scoured the Groups API and forum
   but couldn’t find a way to pull just the names of the Groups w/o a direct database
   query.) 2) nmr_item_visibility – this is the filter modeled after helgatheviking’s
   example in the NMR [FAQ](https://wordpress.org/plugins/nav-menu-roles/faq/). 
   It puts the current user’s groups into an array and then checks to see if any
   of those groups are also in the $item->roles array using array_intersect. Here’s
   the code:
 *     ```
       /*
       Add Group names as custom roles to Nav Menu Roles menu list
       param: $roles an array of all available roles, by default is global $wp_roles
       return: array
       */
   
       function nmr_groups_to_roles ( $roles ) {
       	global $wpdb;
   
       	$order_by = 'name';
       	$order = 'ASC';
   
       	$group_table = _groups_get_tablename( "group" );
       	if ( $groups = $wpdb->get_results(
       			"SELECT group_id FROM $group_table ORDER BY $order_by $order" ))
       		{
       			foreach( $groups as $group ) {
       				$group = new Groups_Group( $group->group_id );
       				// set $key of $roles to $group->name;
       				// this is important because $item->roles is an array of the $key values
       				$roles [$group->name] = $group->name;
       			}
       	}
       	return $roles;
       }
   
       add_filter( 'nav_menu_roles', 'nmr_groups_to_roles' );
   
       /*
       Set visibility of menu item to true, if
       - current user is a member of a group
       - AND that group has been given permission to see the menu item via Menu interface
       param: $visible boolean
       param: $item object, the complete menu object. Nav Menu Roles adds its info to $item->roles
       return: boolean
       */
   
       function nmr_item_visibility( $visible, $item ){
   
       	if( isset( $item->roles ) && is_array( $item->roles ) ){
       		$current_users_groups = array();
   
       		// get current user's groups and put into array
       		$user_ID = get_current_user_id();
       		$user = new Groups_User( $user_ID );
       		$groups = $user->groups;
       		foreach( $groups as $group ) {
       			$current_users_groups[] = $group->name;
       		}
   
       		if (! empty($current_users_groups)) {
       			// if any of the user's groups are among the roles selected via Nav Menu Groups, set visibility to true
       			if ((bool) array_intersect($current_users_groups, $item->roles) ) {
       				$visible = true;
       			}
       		}
        	}
       	return $visible;
       }
   
       add_filter( 'nav_menu_roles_item_visibility', 'nmr_item_visibility', 10, 2);
       ```
   
 *  Plugin Author [HelgaTheViking](https://wordpress.org/support/users/helgatheviking/)
 * (@helgatheviking)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/getting-roles-from-groups-groups-plugin/#post-5086024)
 * [@philzo](https://wordpress.org/support/users/philzo/) – that is so cool… great
   job! thanks so much for sharing it. +100
 *  [genei09](https://wordpress.org/support/users/genei09/)
 * (@genei09)
 * [11 years, 9 months ago](https://wordpress.org/support/topic/getting-roles-from-groups-groups-plugin/#post-5086079)
 * I’m looking to do this same thing, however I’m not incredibly familiar with this
   plugin. Where should this code go?
 *  Plugin Author [HelgaTheViking](https://wordpress.org/support/users/helgatheviking/)
 * (@helgatheviking)
 * [11 years, 9 months ago](https://wordpress.org/support/topic/getting-roles-from-groups-groups-plugin/#post-5086080)
 * I’d probably create a new plugin.

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Getting Roles from 'Groups' (Groups plugin)’ is closed to new replies.

 * ![](https://ps.w.org/nav-menu-roles/assets/icon-256x256.png?rev=2336319)
 * [Nav Menu Roles](https://wordpress.org/plugins/nav-menu-roles/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/nav-menu-roles/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/nav-menu-roles/)
 * [Active Topics](https://wordpress.org/support/plugin/nav-menu-roles/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/nav-menu-roles/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/nav-menu-roles/reviews/)

## Tags

 * [itthinx](https://wordpress.org/support/topic-tag/itthinx/)

 * 9 replies
 * 3 participants
 * Last reply from: [HelgaTheViking](https://wordpress.org/support/users/helgatheviking/)
 * Last activity: [11 years, 9 months ago](https://wordpress.org/support/topic/getting-roles-from-groups-groups-plugin/#post-5086080)
 * Status: not a support question