Three tips for comment theming in Drupal

<?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)));
?>

<?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;
}
?>

Blog Tags: DrupalThemesSEOPHP

Comments

Where do you put both of the above snippets?

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!

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.

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 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