Printing a list of managed groups & user groups that user belongs to.

    • Mr A B
      Participant
      # il y a 1 année et 5 mois

      Can you tell me how I can print a list of user groups and managed groups that a user belongs to. I would like to add these as class names to the body tag so that I can control css styling based on their groups.

    • Matias Larralde
      Maître des clés
      # il y a 1 année et 5 mois

      Hello,

      Can you tell me how I can print a list of user groups and managed groups that a user belongs to

      What you are asking is cuztomization and would require coding knowledge and custom development. We can’t give a full out of the box solution, but find below some tips that should help you or your developer to achieve what you need.

      This is how you can get a list of managed groups IDs belonging to the current user (check customer-area-managed-groups/src/php/managed-groups-addon.class.php for more public functions you could use)

      <?php
      // Get the managed-groups add-on to access required functions
      $mg_addon = cuar_addon('managed-groups');
      
      // The current user ID
      $user_ID = get_current_user_id();
      
      // Get an array of group IDs belonging to that user
      $users = $mg_addon->get_groups_of_user($user_ID );

      For classic groups, you’ll need to use the appropriate class instead (check customer-area-extended-permissions/src/php/user-group/user-group-addon.class.php), by getting the appropriate instance like this instead:

      <?php
      // Get the groups add-on to access required functions
      $gp_addon = cuar_addon('user-group');

      I would like to add these as class names to the body tag so that I can control css styling based on their groups.

      This can be done by using our template system (check our template system documentation). You could copy/edit the templates, use the functions given above, adapt them to your needs and generate some CSS classes depending on what you need.

      Regards.

    • Mr A B
      Participant
      # il y a 1 année et 5 mois

      Thank you for your help. I now have the following code that works well inside my customer-account pages and displayes all the groups a customer is in. Where can I add this code to work accross the entire Customer Area and to add a bodyclass?

      $current_user = $this->get_current_user();
      
      // Get the managed-groups and user-group add-on to access required functions
      $mg_addon = cuar_addon('managed-groups');
      $gp_addon = cuar_addon('user-group');
      
      // The current user ID
      $user_ID = $current_user->ID;
      
      // Get an array of group IDs belonging to that user
      $managedGroups = $mg_addon->get_groups_of_user($user_ID);
      $userGroups = $gp_addon->get_groups_of_user($user_ID);       
      
      // get the group name from each ID
      foreach ( $managedGroups as $managedGroup ) {
      $groupClasses .= str_replace(' ', '', strtolower($managedGroup->post_title)) . ' ';
      }
      
      foreach ( $userGroups as $userGroup ) {
      $groupClasses .= str_replace(' ', '', strtolower($userGroup->post_title)) . ' ';
      }
      
      echo $groupClasses;

       

       

    • Matias Larralde
      Maître des clés
      # il y a 1 année et 5 mois

      Hi,

      You can inject CSS body classes on WordPress by using the body_class hook.

      Also,

      In order to avoid PHP errors when deactivating add-ons, you should make sure that managed-groups and user-group PHP classes exists before executing that code.

      You’ll also probably need to check that you are on a WP Customer Area page.

      You can insert this kind of code in a custom plugin and bind it to the body_class.

      One last tip, this should be better:

      $groupClasses .= sanitize_title($userGroup->post_title)

      Regards.

    • Mr A B
      Participant
      # il y a 1 année et 5 mois

      That seems clear. Thank you so much for your help.

    • Matias Larralde
      Maître des clés
      # il y a 1 année et 5 mois

      You’re welcome 🙂

Vous lisez 5 fils de discussion

The topic ‘Printing a list of managed groups & user groups that user belongs to.’ is closed to new replies.