Drupal: How to Remove the "not verified" Text from Comment Author Names

by Chadwick Wood
March 27th, 2009

If you have comments activated on your Drupal site, and you allow non-members to leave comments, by default Drupal will append "(not verified)" to the names of people who leave comments and aren't logged in to your site. On many sites (like mine, for instance), people aren't even allowed to register accounts, meaning every comment has that pesky "(not verified)" text with it! I find that unfriendly, so here's how you can change that. The culprit behind this behavior is the theme_username() function in the Drupal core. That's the function that formats usernames (including comment author names) for display. Beautifully, though, Drupal's theming system allows you to override this function and make it work how you want. I won't get into the general topic of Drupal theming here, but you'll get an idea of how it works from the task at hand.

To override theme_username(), you need to add a function in the template.php file for the theme that your site is using. If your theme is named "mytheme", for example, the corresponding template.php file is probably found either in /themes/mytheme or, if you're a good Drupaler, in /sites/all/themes/mytheme. Once you've found it, you need to add your override function.

Whenever I'm overriding a function, I generally want it to do what Drupal does by default, with some minor modification, such as what we're doing here. So, I want to use the code from the original function to base my new code off of. The quickest place to find that code is through the Drupal API online. The code for theme_username() can be found here, but I'll be including our modified function below, so no need right now for you to go there.

Here's our modified function. Everything in it is just a copy of the core Drupal code (with the original code comments removed), with one modification (see the code comments):

// I've named the function according to theming standards, replacing "theme" with our theme's name
function mytheme_username($object) {

  if ($object->uid && $object->name) {
    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) {
    if (!empty($object->homepage)) {
      $output = l($object->name, $object->homepage, array('attributes' => array('rel' => 'nofollow')));
    }
    else {
      $output = check_plain($object->name);
    }

    /**
     * HERE I've commented out the next line, which is the line that was adding
     * the unwanted text to our author names!
     */
    // $output .= ' ('. t('not verified') .')';
  }
  else {
    $output = variable_get('anonymous', t('Anonymous'));
  }

  return $output;
}

Once you've saved the changes to template.php, you'll probably have to clear the theme cache to see it take effect. Instructions on doing that can be found here.

Additionally, you can make this function transform and play with names however you want. You could make "Anonymous" comment authors show up with the name "Person Too Busy to Tell Me Who They Are", or convert names to all lower case, or whatever. Go nuts.

Feel free to use the comments section below for questions!