Drupal 6 SEO: Improve SEO and Page Rank by preventing Link Juice Drain

Submitted by Janak on Fri, 06/26/2009 - 12:56

As anyone paying a close attention to their Page Rank and SEO will already know this, too many outbound links from your webpage can drain the "Link Juice" of your webpage andcan hamper the Page Rank and SEO value of your website. This is especially true when you are displaying hundreds of comments on the same page and each comment links to the user homepage (which may be offtopic).

Since I am not an expert in this area, the focus of this post is not to explain or debate about Page Rank, Link Juice, SEO or PageRank sculpting.. If you are worried about "Link Juice" of your node pages getting drained by the above scenario, here is how we can use rel=nofollow method on comments.

Drupal 6 makes this task rather easy by providing very useful hook_preprocess functions..

If (like me) you are using the Gravatar module, insert the following (patched) function to your template.php file (inside your theme folder).

 

function mytheme_preprocess_user_picture(&$variables) {
  $variables['picture'] = '';
 
  if (variable_get('user_pictures', 0)) {
    $account = $variables['account'];
    $picture = NULL;
 
    // If this is a node or comment object, load the user object.
    if (!empty($account->nid) || !empty($account->cid) || empty($account->roles)) {
 
      // If a comment is being edited and previewed, the $account->uid is NULL.
      // @todo Remove when http://drupal.org/node/334826 is fixed in 6.x.
      if (!isset($account->uid)) {
        $account->uid = 0;
      }
 
      $account = user_load($account->uid);
 
      // Load mail/homepage variable from an anonymous comment.
      if (empty($account->mail) && !empty($variables['account']->mail)) {
        $account->mail = $variables['account']->mail;
      }
      if (empty($account->homepage) && !empty($variables['account']->homepage)) {
        $account->homepage = $variables['account']->homepage;
      }
    }
 
    // Load alternate Gravatar e-mail address.
    // @todo Remove this.
    //if (!empty($account->gravatar_mail)) {
    //  $account->mail = $account->gravatar_mail;
    //}
 
    // Decide with picture to use.
    if (!empty($account->picture) && file_exists($account->picture)) {
      // If the user has an uploaded picture, use it first.
      $picture = file_create_url($account->picture);
    }
    elseif (!user_access('use gravatar', $account) || (user_access('disable own gravatar', $account) && isset($account->gravatar) && !$account->gravatar)) {
      // If the user does not have access to use gravatars or has gravatars
      // disabled for their account, use the global default image.
      $picture = _gravatar_get_default_image(GRAVATAR_DEFAULT_GLOBAL);
    }
    else {
      // Otherwise, show a gravatar with the appropraite default picture.
      $picture = _gravatar_get_gravatar(array('mail' => $account->mail));
    }
 
    if ($picture) {
      $alt = t("@user's picture", array('@user' => $account->name ? $account->name : variable_get('anonymous', t('Anonymous'))));
      $variables['picture'] = theme('image', $picture, $alt, $alt, NULL, FALSE);
      if ($account->uid && user_access('access user profiles')) {
        // Create link to the user's profile.
        $attributes = array('attributes' => array('title' => t('View user profile.'), 'rel'=>'nofollow'), 'html' => TRUE);
        $variables['picture'] = l($variables['picture'], "user/$account->uid", $attributes);
      }
      elseif (!empty($account->homepage)) {
        // If user is anonymous, create link to the commenter's homepage.
        $attributes = array('attributes' => array('title' => t('View user website.'), 'rel'=>'nofollow'), 'html' => TRUE);
        $variables['picture'] = l($variables['picture'], $account->homepage, $attributes);
      }
    }
  }
}

 

If you are using the vanilla install of drupal, you can use this:

function mytheme_preprocess_user_picture(&$variables) {
  $variables['picture'] = '';
  if (variable_get('user_pictures', 0)) {
    $account = $variables['account'];
    if (!empty($account->picture) && file_exists($account->picture)) {
      $picture = file_create_url($account->picture);
    }
    else if (variable_get('user_picture_default', '')) {
      $picture = variable_get('user_picture_default', '');
    }
 
    if (isset($picture)) {
      $alt = t("@user's picture", array('@user' => $account->name ? $account->name : variable_get('anonymous', t('Anonymous'))));
      $variables['picture'] = theme('image', $picture, $alt, $alt, '', FALSE);
      if (!empty($account->uid) && user_access('access user profiles')) {
        $attributes = array('attributes' => array('title' => t('View user profile.'), 'rel'=>'nofollow'), 'html' => TRUE);
        $variables['picture'] = l($variables['picture'], "user/$account->uid", $attributes);
      }
    }
  }
}

 

As you will notice, in both the above patches, I have just edited the line below

$attributes = array('attributes' => array('title' => t('View user website.'), 'rel'=>'nofollow'), 'html' => TRUE);

 

It doesnt get easier than this!!