Three tips for comment theming in Drupal


Today I’m just demonstrating a few simple theme adjustments to comments. Comments in Drupal 5 are “not sexy”, out of the box, so in this short post I’m going to illustrate how to:


* change the text on “submitted” lines in comments (just a little)

  • add nofollow to username links – unless you’re feeling generous
  • remove the “not verified” marker from anonymous users

<?php
// change the "submitted by" text to "posted by"
// note that you can alter the date display too, by changing the way
// format_date() is called - see http://api.drupal.org/api/function/format_date/5
$vars['submitted'] = t('Posted by !a on @b.',
                     array(
'!a' => theme('username', $vars['comment']),
                    
'@b' => format_date($vars['comment']->timestamp)));
?>


This first snippet belongs in the ‘comment’ section of your _phptemplate_variables function in template.php; I have an example of this “here.”:http://www.kinetasystems.com/blog/where-do-i-create-phptemplate-variable...


Moving on to that “not verified” business, and the question of holding on to your link juice, adding an overridden theme_username() function is the way to go. Here’s some code, which you can also place in your template.php file in your theme. Remember, if you don’t have that file in your theme, you can just create it yourself. I personally recommend Zen as a starting point for theming.

<?php
// we can't use phptemplate_username as this is already declared in that engine
function mytheme_username($object) { // rename according to your theme

  // this basically means "if the user has an account"
 
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) {  // obviously you could change this value
     
$name = drupal_substr($object->name, 0, 15) .'...';
    }
    else {
     
$name = $object->name;
    }

    if (
user_access('access user prosites/default/files')) {
     
// we could nofollow the internal links too (authenticated user's pages)
      // but there's not really any point - my site doesn't use membership,
      // so usernames are not highlighted -
      // commented line below would do that, though.
      // $output = l($name, 'user/'. $object->uid,
      //     array('title' => t('View user profile.'), 'rel' => 'nofollow'));
     
$output = l($name, 'user/'. $object->uid,
          array(
'title' => t('View user profile.')));
    }
    else {
     
$output = check_plain($name);
    }
  }

 
// if we're entering this func, the user is anon (i.e. we want to nofollow them)
 
else if ($object->name) {
    if (
$object->homepage) {
     
// this is where we're nofollow-ing the external links to comment authors' pages
      // we don't really need to use t() here, as rel=nofollow is language independent
     
$output = l($object->name, $object->homepage, array('rel' => 'nofollow'));
    }
    else {
     
$output = check_plain($object->name);
    }
   
// commenting out this line prevents "teh ugly" in the $submitted text
    // $output .= ' ('. t('not verified') .')';
 
}
  else {
   
$output = variable_get('anonymous', t('Anonymous'));
  }
  return
$output;
}
?>


If you’ve got any other simple tips like these, let me know, so I can use & share those too!


Update: This code works fine under Drupal 6 as well – but don’t forget to clear your theme registry when you’ve modified things in your theme!

Blog Tags: DrupalThemesSEOPHP

Comments

Where do you put both of the above snippets?

Fair point – updated this, thanks! Also, note that the Zen theme now comes with [most of] the second function by default, but only as of very recently. I wrote this article some time previously, but didn’t publish it until today.

The nofollowlist also has a whitelist now...but I guess that's not what you need.

If your Drupal site is adding "nofollow" to links then you should look for it in the input formats section: http://example.com/admin/settings/filters/

One of those probably has an option enabled which is setting the nofollow. If it's not that...please include a link to your site and and describe which of the links on the page is nofollow that you wish was dofollow and I'll try to help.

hmh... but i need to add attribute rel="nofollow" not only to the link in a header of comment
i want to add this attribute to the body of communenet, for example this link to http://ligames.ru is normal without any attributes

Very nice blog. Thank you.

I have no idea about trupal.I wanted to learn trupal.Thanks for your information.

For past few days I am learning drupal.Very interesting one.by reading your post I got one concept.Let me try this out.Let you know the feedback soon.

Any chances to have this code for 6.x? I'm done with my new design, but have to wait until I get this issues taken care of. Otherwise, it will be a PR bleeding. Also, how do you deal with URLs that automatically turn into links like http://www.SpanishSEO.org/ I'd like to apply nofollow to all blog interaction. Thanks for sharing!

Well, in Drupal if you want to stop links automatically linking from within comments, I suggest you switch the default input format from “Filtered HTML” to plain text – you might need to create that manually, not sure. Part of the input format setup that is there by default is “URL filter – Turns web and e-mail addresses into clickable links” – if you disable this on your default filter, that will remove the automatic linking of any URLs embedded in comments.

Also, you could do worse than check out the Better Formats module, which allows you to configure various formats for different input fields, based on role/permission.

As for porting the code to D6, I don’t have time right now, but I would suggest that if you look at the theme function in core and copy it to your theme, you should be able to use the code above to make something useful out it. Ask on the Drupal IRC channels for more help with that if you need it.

Hope that helps.

I'm interested for nofollow. I work on my new site in drupal, but I don't know how to implement nofollow links to comments and other external links on whole site.

Well sir, this code will help you toward nofollowing comments in Drupal 5. I’ve just checked, and it appears to work on Drupal 6 as well!

If you want to nofollow links in content as well, I suggest you check out the nofollow list module, which seems to do most of the work. Of course, any external links that are not in your node content will not be affected.

I just tries this using the blix theme, didn't work. I am a little frustrated, but its my fault for starting with blix instead of something like Zen. Thanks for the info.

I don’t see any reason why this wouldn’t work in any theme, really. Couple of things though: firstly, check any modified function names are correct (verify by checking the code comments above; secondly, check that any functions you add aren’t already in the theme that you’re using (i.e. check there isn’t a duplicate themename_username() function in the template.php of the Blix theme.

Furthermore, you should always refresh your theme registry when you’ve added new things to your theme. An easy way to do this is have admin_menu and devel modules installed – then you can simply jump into the dropdown menu and refresh it from there.

I applied the above code to customize the comments of my blog and it was excellent!
To enable the nofollow tag I used a plugin (Seo Platinum) and also worked very well. Very good Article, very explanatory.

Thank you.

It seems that a lot of the solutions offering are an all or nothing. I know WP has a few plugins that will let you choose on an individual basis if necessary, which links are followed or not, or even follow them in comments after a certain number of approved comments are generated. Anyone know of a Drupal solution like that?

thanks, im using these snippets in one of my older projects.

I need all the help i can get with Drupal, thanks for your article.

Mena from mn web hosting

Thanks for you sharing. I'm done with my new design, but have to wait until I get this issues taken care of.

I'm interested for nofollow. I work on my new site in drupal, but I don't know how to implement nofollow links to comments.

I must admit great skills thanks for sharing this information with us. Your comments on the code are excellent. thanks again.

I applied the above code to customize the comments of my site and it was excellent!

I needed this tip for drupal. Great information here. Thanks a lot for this.

Are this tips available for Drupal 6? Also, is there a way to avoid HTML formating in the content field?

Thanks for your help!

Melissa, web dev at SEInsights.com

Melissa, I'm pretty sure these tips are interchangeable with Drupal 6 theming, so you should be ok there.

To set a default input format on the comment box and thereby achieve a similar effect that I have on this site, you can modify the form thus:

<?php
function mymodule_helper_form_alter(&$form, $form_state, $form_id) {
  if (
$form_id == 'comment_form') {
   
// only do this for anonymous users
   
if (!$user->uid) {
     
// change the default input filter to '1' (can't remember, but not HTML)
     
$form['comment_filter']['format'] = filter_form(1);                             

     
// turn off the display of the input format element
     
$form['comment_filter']['format']['#access'] = FALSE;                           
    }
  }
}
?>

thanks for the info

Thanks a ton. I've been looking to increase the visuals of my blog, and not to mention your comment section is stunning. Great job

Very informative tips about the comment theming. Bookmarked.

If you want to nofollow links in content as well, I suggest you check out the nofollow list module, which seems to do most of the work. Of course, any external links that are not in your node content will not be affected.

Add new comment