Add user's role as a class to all user links My goal is to attach a users role as a class to their user name anywhere it is displayed so I can color the link based on role.

I can get roles of every displayed user link using MYTHEME_username_alter:

function MYTHEME_username_alter(&amp;$name, $account) {
  $user = user_load($account-&gt;uid);
  // I can get the roles of each user link through $user
}



Is this the most efficient way to get the role for each user link?
What is the best way to go about adding the role as a class to every user link?
Am I way off track and overlooking an easier way to complete my goal?
I don't think you can do it with username_alter. You can only modify the user name string in there. Its probably better to use preprocess. In your theme template.php. Do something like this:

function YOUR_THEME_NAME_preprocess_username(&amp;$variables) {
  // Username is linked, use the link_attributes to inject the class or any other attribute
  if (isset($variables['link_path'])) {
    $variables['link_attributes']['class'][] = 'SOME_CLASS';
  }
  // Username is NOT linked, maybe do something else or nothing
  else {
    $variables['attributes_array']['class'][] = 'SOME_CLASS';
  }
}