• Resolved entropea

    (@entropea)


    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

    (@proaktion)

    Hi there!

    You are confusing roles and groups ??

    You would have to obtain the groups that the user belongs to (see https://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 entropea

    (@entropea)

    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.