I've added few more fields in "Administration » Configuration » People » Account settings » Manage fields". See the attached screenshots for more details.
Now I want to display user's First name & Last name in page.tpl file. I know that name can be diplayed like this "$user->name". But I want to know how other filed values can be displayed. I want to display the fileds First name, Last name, Phone, Address
3 Answers
If you want to display first name
and last name
of the logged in user, then you can follow the below code snippets.
global $user;
$account = user_load($user->uid);
print "First name:" . $account->field_f_name[LANGUAGE_NONE][0]['value'];
print "Last name:" . $account->field_l_name[LANGUAGE_NONE][0]['value'];
The global $user
object does not load the fields which are attached to user profile. So, We need to use function user_load()
in code.
If you want to display First Name
and Last Name
of the user profile, then you need get uid of the user from the arguments.
$uid = arg(1);
$account = user_load($uid)
A better way is using the Field API.
I'm not sure which variables are you handling on the page.tpl.php
As harsal says, you can use the global variable $user, but to access each field of the current user, you could do it like this (for example for phone would be):
echo render(field_view_field('user', $user, 'field_phone', array('label' => 'hidden')));
Additional tip: to protect each field against XSS attacks, you cloud use the Drupal function filter_xss()
The following can be implemented to get the user details
global $user;
$user_load = user_load($user->uid);
$user_load->field_f_name[LANGUAGE_NONE][0]['value'] ;
$user_load->field_l_name[LANGUAGE_NONE][0]['value'] ;
$user_load->field_phone[LANGUAGE_NONE][0]['value'] ;
$user_load->field_address[LANGUAGE_NONE][0]['value']