I am making a profile page for users on my site, and would like to print out their username. If I do print $user->name it will just print out the username of the currently logged in user, I want the username of the profile I am looking at. Thanks.

  • 2
    Maybe it's just me, but I don't really understand why you are "making a profile page". Every user has a profile page out-of-the-box at example.com/user/[uid]. That page displays the user name by default. Why don't you take that page as a starting point? – marcvangend Dec 9 '11 at 11:49
  • My site requires more complexity on the profile page than the default offers. On the default profile page, it uses the $title variable to display the username which is in the page.tpl file. My theme calls to display the username differently, and requires me to have it printed in the user-profile.tpl file. – oobie11 Dec 12 '11 at 2:10
  • I'm still not convinced you need a completely custom profile page. I'll post an answer below. – marcvangend Dec 12 '11 at 8:49

Modules can implement hook_username_alter() to alter the displayed username of any user, so you cannot simply load a user account object with user_load() and print its name property. Instead, the format_username() function should always be used.

In the user-profile.tpl.php template, you should be able to retrieve the viewed user account object in $elements['#account']. If the code is executed in the context of an user page (ie. path starting with user/%nid), you can always retrieve the currently viewed user with menu_get_object().

$account = $elements['#account'];
// OR
$account = menu_get_object('user');
if ($account) {
  $username = format_username($account);
  • This doesn't explain what the OP is reporting: " If I do print $user->name it will just print out the username of the currently logged in user." – apaderno Dec 9 '11 at 18:12
  • Using menu_get_object('user') to retrieve the viewed user account (as opposed to currently logged user) should solve the original reported issue. The OP didn't mention in which template or hook the code was used. – Pierre Buyle Dec 12 '11 at 10:58

It depends on how you build the profile page (which modules you use), but usually you will get the user info on profile templates by accessing $account, while $user is the logged in user.

So, in short, $account->name should be what you are after. Again, this depends on how you create the profile page. For a profile page built with Views, this would not help, but you could add the name in the View itself.

  • I have tried using the $account-name variable, but its just giving me a undefined variable error. Im trying to print it in my user-profile.tpl.php file. I'm not using views, just strait themeing. – oobie11 Dec 12 '11 at 2:14
  • 1
    I think you missed a small detail, it's not $account-name but $account->name. $account is the user object and name is a property of that object. – marcvangend Dec 12 '11 at 9:44

You write that your site "requires more complexity on the profile page than the default offers", but I still wouldn't consider that a reason to not use the user/[uid] page. Allow me to explain why.

A module like RealName gives you a lot of control over the displayed user name, not just on the profile page, but everywhere in your site. That means it's a more consistent solution than template-specific code.

The Field system in D7 provides a lot of options to further customize the default profile page, especially when combined with even more flexible and powerful modules such as Entity Views Attachment and Display Suite.

It's not my goal to convince you, I just want to make sure you have all information you need to choose your optimal solution. Hope that helps.

  • 1
    Sorry, my wording was off. I am using the "default" drupal profiling system. I am just heavily theming it, and wanted to stay within one .tpl file (the user_profile.tpl). In my theme, I do not use the <h1 class="title" id="page-title"></h1> on the user page, and that is where ,by default, the username is printed. I needed a way to print the username in a different area of the page. Thank you for trying to steer me in the right direction and provide me with some new info though, it is much appreciated. – oobie11 Dec 12 '11 at 9:21
  • OK, that explains. Thanks for the feedback and good luck with your site. – marcvangend Dec 12 '11 at 9:39

The way I figured out is to use print $elements['account']->name. I found this out with the help of the Devel module, using dsm($elements) in the user-profile.tpl.

  • 2
    $elements['account'] will be the currently viewed user object. As explained in my answer, don't simply print it's name property but use the format_username() function. Unless your really want to display the 'plain username' and not the 'display username'. And dsm() is deprecated, you should use dpm(). – Pierre Buyle Dec 12 '11 at 8:42

I guess it should be simple enough with the use of user_load(), and using the loaded object to print the name.

  • I didn't down-vote the answer, but what reported here is not necessary: The user object for user associated with the profile is already passed to the profile template, and any related functions. – apaderno Dec 9 '11 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.