I want to obtain the organic groups role-id of a node/comment-author in node.tpl.php and comment.tpl.php.

I can't find an array that provides this information.

I want to add a class based on the authors membership type.


<div id="header" class="<?php if ($author->rid == 7): print 'class-7'; endif; ?>">

How to do this for nodes and comments?

Note: for normal (non-organic-group) roles I use:

<?php if(in_array('administrator', $user->roles)): print 'admin-type'; endif; ?>

Which works ok, I want similar usage for organic group roles.

  • Nevermind, solved it with this page: api.drupalecommerce.org/api/og/og!og.module/7.x-2.x
    – Marcel
    Commented Nov 19, 2013 at 15:59
  • Instead of editing your questions and/or commenting, you can make it into an answer by filling out the Your Answer section below.
    – mpdonadio
    Commented Nov 19, 2013 at 16:58
  • I cant I have not enough reputationpoints and have to wait 8 hours before I can answer my own question.
    – Marcel
    Commented Nov 19, 2013 at 17:25
  • 1
    If you can remember to answer after the 8 hour window, that would be great.
    – mpdonadio
    Commented Nov 19, 2013 at 17:49

1 Answer 1


Found the anwser: This goes in node.tpl.php (tested on Drupal 7, Organic groups 7.x-2.3)

<?php if (og_context()) {
  $group = og_context();
  $userid = user_load($node->uid);
  $roles = og_get_user_roles('node', $group[gid], $uid = $userid->uid, $include = FALSE);

Then use it with something like:

<?php if(in_array('test member', $roles)): print '<span>Testuser</span>'; endif; ?>

For comments it's a little different:

<?php if (og_context()) {
  $group = og_context();
  $roles = og_get_user_roles('node', $group[gid], $uid = $comment->uid, $include = FALSE);

Works only on pages where groupcontext is available!

  • 1
    Hello. Code-only answers are of pretty low value: if code cease to work (due to API changes for example), they are simply worthless, and if it does work, it leaves future readers clueless about why and how. Could you please expand it?
    – Mołot
    Commented Nov 20, 2013 at 9:25
  • please add description and steps and also where to add this code.
    – Bala
    Commented Nov 20, 2013 at 9:46
  • Done, you are right, I hate it when I find answers that work on older versions only.
    – Marcel
    Commented Nov 20, 2013 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.