Drupal Theme Hooks: a Simple Example

By default, Drupal adds the text "Not Verified" to comments from unregistered users. It also adds the rel="nofollow" attribute to the links they provide. I didn't like either one of those things so I set out looking where to fix it. It turns out that both these bits happen inside the function theme_username(). This function is hookable. That is to say, this function can be replaced by a custom function in a theme.

My current theme (at the time of writing) is Interactive Media (here's a demo). I like the look of it but of course I'm going to continue tweaking and rewriting. This theme uses the phptemplate engine. Why am I telling you this? Because it determines the name of the function that will override the default theme_username(). I called mine phptemplate_username() - the recommended practice. If I didn't know that my theme is using the phptemplate theming engine then I could equally well call it interactive_media_username().

Now that the name is settled, just grab template.php from the them and add the function. In my case, as I expect is the case for many others, I started from the default implementation of theme_username(). Just copy the function body & paste it in with the new name. The subtle detail to be aware of here is that you're inheriting any bugs (security holes and whatever) that the default implementation has when you do this. I suppose if you're really on the ball then you can make note of these hooks and pay attention in future patches.

My changes to remove the nofollow and "not verified" text are summarized below. Since they're both just deletions, I just commented them out here (look for **).

function phptemplate_username($object) {
  if ($object->uid && $object->name) {
    // Shorten the name when it is too long or it will break many tables.
    if (drupal_strlen($object->name) > 20) {
      $name = drupal_substr($object->name, 0, 15) .'...';
    else {
      $name = $object->name;

    if (user_access('access user profiles')) {
      $output = l($name, 'user/'. $object->uid, array('attributes' => array('title' => t('View user profile.'))));
    else {
      $output = check_plain($name);
  else if ($object->name) {
    // Sometimes modules display content composed by people who are
    // not registered members of the site (e.g. mailing list or news
    // aggregator modules). This clause enables modules to display
    // the true author of the content.
    if (!empty($object->homepage)) {
      // ** rjr just remove the extra attributes to get rid of nofollow
      // was: $output = l($object->name, $object->homepage, array('attributes' => array('rel' => 'nofollow')));
      $output = l($object->name, $object->homepage);
    else {
      $output = check_plain($object->name);
      // ** rjr this line appends "not verified", delete it
      // was: $output .= ' ('. t('not verified') .')';
  else {
    $output = variable_get('anonymous', t('Anonymous'));

  return $output;

This isn't exactly the code that's running on latenightpc.com but it's close. The other changes I made were around the idea of showing a homepage link for registered users. Those changes might not work yet. Basically I noticed that the way this code works, anyone who is a registered user doesn't get a link back to their homepage and the reverse is true for unregistered users. I don't exactly believe that "links are the currency of the web" but this balance just doesn't seem right. I mean, linking to a user's homepage is a way of identifying them universally. Linking to a profile page only identifies them in the scope of the current site.

My implementation's not perfect but what I'm trying to do is give registered users an icon (maybe an avatar later) with a link to their profile page and keep linking their names to their homepages. Then a user's homepage link is treated as the universal identifier I see it to be, with a little perk for registered users.

After making these changes to your template.php, don't forget to flush the page cache on Drupal. This is in the Performance section of the Admin pages. After testing stuff on your development server (of course you set up a development server, right?), upload to the real site and do the same, go to admin/settings/performance and click "Clear Cached Data." Otherwise you may not see any changes on your site.

Your rating: None Average: 3.7 (27 votes)

and click "Clear Cached Data." Otherwise you may not see any changes on your site.

omg that's what you were twittering about. heh. I won't say it's never happened to me. How many hours did you spend?