Code Snippets
Assigning a user to a group
Groups_User_Group::create( array( 'user_id' => $user_id, 'group_id' => $group_id ) );
Removing a user from a group
Groups_User_Group::delete( $user_id, $group_id );
Get all users in a group
$group = new Groups_Group( $group_id );
$users = $group->users;
Get all groups the current user belongs to
$groups_user = new Groups_User( get_current_user_id() ); // get group objects $user_groups = $groups_user->groups; // get group ids (user is direct member) $user_group_ids = $groups_user->group_ids; // get group ids (user is direct member or by group inheritance) $user_group_ids_deep = $groups_user->group_ids_deep;
Does the current user belong to the Foobar group?
$is_a_member = false; require_once( ABSPATH . 'wp-includes/pluggable.php' ); if ( $group = Groups_Group::read_by_name( 'Foobar' ) ) { $is_a_member = Groups_User_Group::read( get_current_user_id() , $group->group_id ); }
Does the current user have the make_donuts capability?
require_once( ABSPATH . 'wp-includes/pluggable.php' );
$groups_user = new Groups_User( get_current_user_id() );
$can_make_donuts = $groups_user->can( 'make_donuts' );
Is a post restricted by group?
Assuming that you have the $post_id of the post for which you would check whether it is restricted, you can obtain the group IDs of the groups that are restricting access to it:
$group_ids = Groups_Post_Access::get_read_group_ids( $post_id );
if ( count( $group_ids ) > 0 ) {
// $group_ids holds one or more IDs of the groups that restrict read access to the post
}
How to use Groups’ API to protect content in templates
You can see how you can protect content within templates, using appropriate API functions provided by Groups in our Groups Twenty Twenty child theme.
This child theme exposes excerpts to guests, while showing full content to registered users. Note that users with an account automatically belong to the Registered group, you can of course use a different group as needed. We recommend to fork the repository and adapt it as desired.
GitHub
Groups – The Groups repository on GitHub.
Groups Premium Menu Hide – An example of hiding menu items for members of a Premium group.
Groups Utilities – Set of utilities and extras for itthinx’s Groups
Users Auto Group – Assign automatically new users to a specific group.
Affiliates Auto Group – Add new affiliates to a specific group.
Groups Comments Filter – Left comments in pages where the user has group access.
Groups List Users – Creates a shortcode that lists users of a specific group.
Groups Twenty Twenty – An example theme illustrating content protection in templates, a Twenty Twenty Child Theme.