function theme_username

You are here

7 theme_username($variables)
4.7 theme_username($object)
5 theme_username($object)
6 theme_username($object)
8 user.module theme_username($variables)

Returns HTML for a username, potentially linked to the user's page.


$variables: An associative array containing:

  • account: The user object to format.
  • name: The user's name, sanitized.
  • extra: Additional text to append to the user's name, sanitized.
  • link_path: The path or URL of the user's profile page, home page, or other desired page to link to for more information about the user.
  • link_options: An array of options to pass to the l() function's $options parameter if linking the user's name to the user's page.
  • attributes_array: An array of attributes to pass to the drupal_attributes() function if not linking to the user's page.

See also



Related topics

23 theme calls to theme_username()
blog_page_user in modules/blog/
Menu callback; displays a Drupal page containing recent blog entries of a given user.
comment_admin_overview in modules/comment/
Form builder for the comment overview administration form.
comment_form in modules/comment/comment.module
Generate the basic commenting form, for appending to a node or display on a separate page.
contact_personal_form in modules/contact/
Form constructor for the personal contact form.
dblog_event in modules/dblog/
Page callback: Displays details about a specific database log message.

... See full list


includes/, line 2235
The theme system, which controls the output of Drupal.


function theme_username($variables) {
  if (isset($variables['link_path'])) {
    // We have a link path, so we should generate a link using l().
    // Additional classes may be added as array elements like
    // $variables['link_options']['attributes']['class'][] = 'myclass';
    $output = l($variables['name'] . $variables['extra'], $variables['link_path'], $variables['link_options']);
  else {
    // Modules may have added important attributes so they must be included
    // in the output. Additional classes may be added as array elements like
    // $variables['attributes_array']['class'][] = 'myclass';
    $output = '<span' . drupal_attributes($variables['attributes_array']) . '>' . $variables['name'] . $variables['extra'] . '</span>';
  return $output;


This print the current logged in user's with link:

global $user;
t('!username logged in', array('!username' => theme('username', array('account' => $user)))),

Setting values into $variables['name'] or $variables['extra'] (array('name' => ..., 'extra' => ...) ) won't work under the default implementation, due to template_preprocess_username(). It has code:

$variables['extra'] = '';
$name = $variables['name_raw'] = format_username($account);
$variables['name'] = check_plain($name);

This code overrides anything set in $variables['name'] and $variables['extra'].

The only ways I can see to put in a custom name would be to either

  1. Remove the preprocess function from the registry so it doesn't run
  2. Change the name property on the account before sending it to $variables['account'];
  3. Make up some other properties in the $variables array (like 'name_real' and 'other_real') to send in a custom implementation of theme_username and use them.

None of these options are ideal.

Edit: $variables['link_path'] is also overridden.

If you just want custom output then you need to override this function in your theme. I tried overriding the theme_username function and it works as expected.
Please refer for more information.

My point is that, if using the theme_username's default implementation does not respect the values sent in the associative array. If I have

('username', array('name' => 'Custom Name'));

that "Custom Name" will not be displayed because the $variables array will be run through template_preprocess_username() and have its array's values will be altered. (Same with $variables['link_path'] and $variables['extra'].) Yes, I could make a custom theme override function for theme_username() and template_preprocess_username() and make it work, one way or another, but in my opinion, if the default implementation isn't going to work the way it is documented, either the function should be made so it does work, or the documentation should be updated to state that these values are set in the preprocess function, not by the associative array sent to the function.

To change the default display of your user account, change it with hook_username_alter

Issues should be posted in the issue queue, not here.

Suppose that someone suggests (or demands) that you use theme_username() instead of $user->name because it's the Drupal way.

Of course, you are not supposed to invoke theme_username() directly because that is NOT the Drupal way. Instead use...

theme('username', $variables);

This may not work as you expect. So WTF is $variables anyway? Here is what you might have to do to get things to actually work...

* Implements hook_block_view_alter().
* Changes the block title on the User menu and Navigation block. D6 already had
* this feature, this code adds it to D7.
function mymodule_block_view_alter(&$data, $block) {
  global $user;

  if ($block->delta == 'user-menu' || $block->delta == 'navigation') {
    $uid = $user->uid;
    $account = array('account' => user_load($uid));
    $data['subject'] = isset($user->name) ? theme('username', $account) : $data['subject'];

For those of you that are asking themselves, after looking at the code snippet above; WTF? -- Hey don't shoot me, I am only the messenger.

Welcome to the Drupal Developer Experience (DX)!

If you cut out the code specific to your implementation there then the remaining bit is:

('username', array('account' => $user));

Which is exactly like calling every other theme, so I'm not following where the problem comes from. I'm not sure where you got that code snippet, but it has a lot of superfluous stuff in it to start with before you even get to the call to theme().