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
Thanks for this!
Thank you. It's working in my blog.
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.
Thank you!
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!
Better than anything I found on the Drupal site. Thank you!
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. : )
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
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.
Works fine. Thanks a lot
Post new comment