Community Documentation

Comment author name as link

Last updated October 12, 2012. Created by BWPanda on October 12, 2012.
Log in to edit this page.

Use the following PHP code in a custom token to display the comment author's name as a link to either their homepage or their user profile. If they didn't enter a homepage URL and they're an anonymous user, or if accessing user profiles is not allowed, the comment author's name will be displayed without a link.

Set the 'token type' to 'Comments'.

<?php
$comment
= $data['comment'];

$name = $comment->name;
$homepage = $comment->homepage;
$uid = $comment->uid;

if (empty(
$homepage)) {
  if (
$uid > 0 && user_access('access user profiles')) {
    return
l($name, 'user/' . $uid);
  }
  else {
    return
$name;
  }
}
else {
  return
l($name, $homepage);
}
?>

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 7.x
Audience
Programmers, Site administrators, Site builders
Level
Intermediate
Keywords
author, comments, homepage, link, profile
Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here