I would like to see users' information in a page node. I created some fields in a user profile like City and state, but I am unable to see them when I create a page and input PHP in the body text. I created a field called profile_city and I am trying to dereference it by entering

<?php 
global $user
print t("$user->profile_city");
?>

This does not seem to work, however. I know that something similar works because I can this code to user_profile.tpl.php and it works just fine. What am I doing wrong?

Comments

nevets’s picture

Profile data not loaded by default, need to call profile_load_profile() like this

<?php
global $user
profile_load_profile($user);
print t("$user->profile_city");
?>
AvantSavant’s picture

Thank you. That's one of those bits of information that I haven't come across yet. Do you know of any page that states this?

nancydru’s picture

It might work better like this: (BTW, this does not require monkeying with theme code, this can be in any page.)

<?php
  global $user
  $me = user_load($user->uid);
  print t("$me->profile_city");
?>

For documentation, try http://api.drupal.org/api/HEAD/function/user_load

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

nevets’s picture

When calling user_load() you need to pass an array, not just a uid, like this

$me = user_load(array('uid' => $udser->uid));
nancydru’s picture

AvantSavant’s picture

It seems that I need to add

<?php global $user; ?>

when I add my own php to a page I create within the Drupal web interface. However, I don't seem to need this line when I edit the php files directly, such as page-front.tpl.php or user_profile.tpl.php. I have not found an explanation for this. Does anyone know?

nevets’s picture

If you look in the handbook at http://drupal.org/phptemplate, you can see links for some of the common tpl.php files which list variables you can expect to find set.

In the case of user_profile.tpl.php, the $user variable is not the global variable but a "local" variable that reflects the user information for the account being viewed.

Normally page.tpl,php files (such as page-front.tpl.php) do not normally have $user set so I wonder if it being declared early on in the file (it is not listed in the handbook and I can not find it being set by phpthemplate.engine which sets the default variables, it could be being set in the template.php for the theme you are using)