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

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!

Comments

Pat's picture

Thanks for this!

Aline Freitas's picture

Thank you. It's working in my blog.

Torsten's picture

Thanks for your HowTo. I searched for a solution to remove the "not verified" text and the rel=nofollow for visitor links, with your hint I was able to solve both.

Anonymous's picture

Thank you!

eric's picture

Thanks for posting this. It's near the top of the results in Google for "(not verified)" (which is lucky, since there are over 8,000,000 results and Google ignores "(" and ")").

To clarify, in D6 it's not sufficient to flush the theme registry -- you've got to flush all caches. (I think that's non-canonical, but it's true on the D6.15 site I just modified.) So if it doesn't seem to work -- make sure you've cleared the system caches!

Scotty's picture

Better than anything I found on the Drupal site. Thank you!

Karu's picture

It works! Thanks for the detailed explanation which helps a newbie like me understand what I'm doing. Can't take blind-faith no more. : )

Anonymous's picture

very nice man it worked flawless.... but there is this other solution i found out that also worked great

function mytheme_username($object) { return str_replace(' ('. t('not verified') .')', '', theme_username($object)); }

and have tried this also and it also works just fine.

PS: this solution is not by me :P

Chadwick Wood's picture

Anonymous (nice name :), yeah, that alternative solution will work, but it's less flexible, and is in my mind a bit backwards in its approach... it calls the default function that adds the text you don't want, then removes that text.

That being said, it is a short fix.

Warmiboa's picture

Works fine. Thanks a lot

Juc1's picture

Sorry for being cluless but I am doing something wrong.

My comments presently say... Posted by George (not verified) on Tuesday December 7th ...but I want them to say... Posted by George on Tuesday December 7th

I tried to follow the instructions above but the result was...

Posted by on Tuesday December 7th

I think I am supposed to change this line...

function mytheme_username($object) {

So if my theme is called 'sunset' would my code be

function sunset_username($object) {

Thanks

Chadwick Wood's picture

Juc1, yes that looks right. You might also make sure to flush the theme registry when you first add this function in your theme, to make sure your theme code is being used.

Tifoo's picture

Hello,

Thank you very much for the explanation, very simple. But as Juc1, I can't make it work, I use the garland theme (the default one) and override the function by calling it ' function garland_username($object) ' I tried with a 'function Garland_username($object)', I cleared the cache in the performance administer page but nothing to do I still have (not verified) everywhere :(. Someone has an idea?

Thank you!

Chadwick Wood's picture

Tifoo, I think that calling it garland_username should work fine, however you really shouldn't alter the built-in themes that come with Drupal. Instead, you should create your own theme that is a copy of that theme, like this article describes. I'd suggest going that route, and then seeing if you're still having the same problem.

Jack's picture

Great tip, and worked flawlessly on my D6 site; thanks for this!

Michelle H's picture

Worked like a charm with my custom theme and D6!

willem's picture

didn't work for me in D7

the object array has an object called 'account'.. so replacing the $object with $object['account'] did the job for me.

something like this...

function mytheme_username($object) {

if ($object['account']->uid && $object['account']->name) { if (drupal_strlen($object['account']->name) > 20) { $name = drupal_substr($object['account']->name, 0, 15) .'...'; } else { $name = $object['account']->name; }

if (user_access('access user profiles')) {
  $output = l($name, 'user/'. $object['account']->uid, array('attributes' => array('title' => t('View user profile.'))));
}
else {
  $output = check_plain($name);
}

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

lopar's picture

hi, can you please write steps on how to remove or change string 'Anonymous' inside the Name box? Thank you

ruchi's picture

Want to know how you have implemented this comment form of drupal and display of comment in this page....its very urgent for my site...please reply

Chadwick Wood's picture

Ruchi, you just need to edit the content type you want to show comments form, and look for the setting labeled "Location of comment submission form". There, you can specify to show the comment form on the same page as the node, rather than a separate page.

Chadwick Wood's picture

lopar, you can go to /admin/settings/site-information in your Drupal site and there's a setting there for "Anonymous user" that controls what that default name is in the Comments form. You can change in there. I'm not sure that you can make it blank, though.

ruchi's picture

Thanks Chadwick Wood for responding here

GeekDrop.com's picture

Tifoo: For the Garland theme you would name the function "function phptemplate_username($object)" in the template.php.

Add new comment

Subscribe to Coffeeshopped Blog