Title: Add groups as body classes
Last modified: April 1, 2020

---

# Add groups as body classes

 *  Resolved [Darren](https://wordpress.org/support/users/entropea/)
 * (@entropea)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/add-groups-as-body-classes/)
 * Hi, I was wondering if someone could share some code that would add the users
   Groups to the body classes, similair to this code:-
 *     ```
       add_filter('body_class', function($classes) {
           global $current_user;
   
           foreach ($current_user->roles as $user_role) {
               $classes[] = 'role-'. $user_role;
           }
   
           return $classes;
       });
       ```
   

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

 *  Plugin Author [Kento](https://wordpress.org/support/users/proaktion/)
 * (@proaktion)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/add-groups-as-body-classes/#post-12632166)
 * Hi there!
 * You are confusing roles and groups 🙂
 * You would have to obtain the groups that the user belongs to (see [http://docs.itthinx.com/document/groups/api/examples/](http://docs.itthinx.com/document/groups/api/examples/)
   for some examples) and then add the classes based on the group names.
 * Also don’t forget to esc_attr() those names …
 * Cheers
 *  Thread Starter [Darren](https://wordpress.org/support/users/entropea/)
 * (@entropea)
 * [6 years, 4 months ago](https://wordpress.org/support/topic/add-groups-as-body-classes/#post-12639329)
 * Tanks Kento, so the code looks like this:-
 *     ```
       //add role and group classes to body for users
       add_filter('body_class', function($classes) {
           global $current_user;
   
           foreach ($current_user->roles as $user_role) {
               $classes[] = 'role-'. $user_role;
           }
   
       	$groups_user = new Groups_User( get_current_user_id() );
       	foreach ($groups_user->group_ids as $user_group_id) {
               $classes[] = 'group-'. $user_group_id;
           } 
           return $classes;
       });
       ```
   

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

The topic ‘Add groups as body classes’ is closed to new replies.

 * ![](https://ps.w.org/groups/assets/icon-256x256.png?rev=983146)
 * [Groups - Memberships and Access Control](https://wordpress.org/plugins/groups/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/groups/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/groups/)
 * [Active Topics](https://wordpress.org/support/plugin/groups/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/groups/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/groups/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [Darren](https://wordpress.org/support/users/entropea/)
 * Last activity: [6 years, 4 months ago](https://wordpress.org/support/topic/add-groups-as-body-classes/#post-12639329)
 * Status: resolved