Customizing the user profile layout

(Thanks to Dublin Drupaller for starting this section of the handbook)

The PHP Snippets below are intended for use within a customized USER PROFILE page that simply enables you to "pull" specific content from your drupal database specific to a particular user and display it in the way you want.

They are intended for use with a phptemplate based theme and for Drupal site developers who do not have php programming knowledge but want to push out the boundaries of user profile pages and control precisely how they look.

See also Advanced Profile Kit for an alternate method of sprucing up your user profile pages.

Simple step-by-step instructions are provided.

The concept

Drupal is an extremely powerful tool for building online communities, in particular, allowing users to submit their own content to a community hub. A good illustration of this working well online might be the World famous myspace.com site, where bands/artists are able to submit content into their own page.

Drupal has all the tools available to create your own myspace.com style community hub.

These snippets are intended as a mini-repository and as an aid for site designers without php programming skills to create sophisticated User Profile Pages for members of their community.

customized User Profile Pages maybe applied to many applications. myspace.com is primarily a site for artists & bands, but, similar techniques could be used for other applications such as a rzye.com (Drupal powered community hub) style professional networking hub or terminus1525 (Drupal powered community hub) for studios.

Getting Started - (Drupal 4.x and Drupal 5.x)

Step 1 - is to override the default User Profile page layout by uploading the special template.php file to your active theme folder.

<?php
/**
* Catch the theme_user_profile function, and redirect through the template api
*/
function phptemplate_user_profile($account, $fields = array()) {
 
// Pass to phptemplate, including translating the parameters to an associative array. The element names are the names that the variables
  // will be assigned within your template.
  /* potential need for other code to extract field info */
 
return _phptemplate_callback('user_profile', array('account' => $account, 'fields' => $fields));
  }
?>

If you already have a template.php file in your active theme folder, simply add the above to the existing template.php file and upload it.

Step 2 - (Drupal 4.x and 5.x) is to create your customized user_profile.tpl.php file and upload that to your active theme folder.

If you're starting from scratch, simply open notepad.exe or a similar text editor and paste in the snippets linked below to build your custom user profile page. Save it with the user_profile.tpl.php filename and upload it to your theme folder along with the template.php file.

Once you have got started with your first user_profile.tpl.php file, you can experiment with adding in more snippets or including HTML layout controls to get a feel for the flexibility this allows.

Getting Started - (Drupal 6.x)

Step 1 - is to override the default User Profile page layout by uploading a custom user-profile.tpl.php* file to your active theme folder.

Drupal will automatically detect the presence of your custom user-profile.tpl.php and override the default user profile layout. To make this happen, you need to rebuild the theme registry, which you can do by clearing the caches (for example using the button on the admin/settings/performance page), or simply by visiting the admin/build/modules page.

* note that in Drupal 6.x, your custom user profile layout file name uses a hyphen, instead of an underscore.

Step 2 - is to customize your user-profile.tpl.php layout file.

By default, all user profile data is printed out with the $user_profile variable. If there is a need to break it up you can use $profile instead.

As an example, the following snippet inserted in your custom user-profile.tpl.php will display the default user profile layout.

<div class="profile">
  <?php print $user_profile; ?>
</div>

Available variables:

  • $user_profile: All user profile data. Ready for print.
  • $profile: Keyed array of profile categories and their items or other data provided by modules.

To check for all available data within $profile, insert the following snippet at the bottom of your custom user-profile.tpl.php.

<div>



<h2>Available variables</h2>
<p>The following is a list of variables that is available to your custom <strong>user-profile.tpl.php</strong>.</p>
<?php print '<pre>'. check_plain(print_r($profile, 1)) .'</pre>'; ?>
</div>

Step 3 - Load profile variables

If you want to load the profile variables (profile module) you can use this code in user-profile.tpl.php

<?php
profile_load_profile
($user);
// now you can call the profile field like profile_firstname
echo $user->profile_firstname;
?>

How to use these snippets

Simply copy and paste these snippets into your custom user profile layout file and upload it to your active theme folder. Check to make sure that the snippet you are using is compatible for the version of Drupal you are using.

It's recommended that you test your customized user_profile.tpl.php(Drupal 4.x or Drupal 5.x) or user-profile.tpl.php file (Drupal 6.x) on a test installation before adding to a live site.

Adding new snippets

Simply click on the ADD NEW CHILD PAGE link below and create a new handbook page. Include any dependencies, such as which version of Drupal you have tested the snippet with or extra modules that need to be enabled for the snippet to work.

PLEASE NOTE! The following snippets are user submitted. Use at your own risk! For users who have setup drupal using an alternate database to the default (MYSQL), please note that the snippets may contain some database queries specific to MYSQL.

using Imagecache Profiles

liquidcms - August 14, 2008 - 04:57

If you are using http://drupal.org/project/imagecache_profiles AND this approach explained here to having a user profile page template; you may want to try adding this:

<?php
 
if (module_exists('imagecache_profiles'))  {
    print
phptemplate_user_picture($user);
  }
?>

to your tpl file.

Peter Lindstrom
LiquidCMS - Content Management Solution Experts

You need to change the name

zanforlin - September 25, 2008 - 13:40

You need to change the name of $user variable to $account (for example), so you can print the user picture in your user profile with

<?php {print theme('user_picture', $account);} ?>

If you try to print a "$user" variable in your user profile, you´ll get the information about the user logged in.

<?php
/**
* Catch the theme_user_profile function, and redirect through the template api
*/
function phptemplate_user_profile($account, $fields = array()) {
 
// Pass to phptemplate, including translating the parameters to an associative array. The element names are the names that the variables
  // will be assigned within your template.
  /* potential need for other code to extract field info */
return _phptemplate_callback('user_profile', array('account' => $account, 'fields' => $fields));
  }
?>

Typo error

jessicakoh - January 10, 2009 - 17:49

Below is the correct one.

<?php
/**
* Catch the theme_user_profile function, and redirect through the template api
*/
function phptemplate_user_profile($account, $fields = array()) {
 
// Pass to phptemplate, including translating the parameters to an associative array. The element names are the names that the variables
  // will be assigned within your template.
  /* potential need for other code to extract field info */
return _phptemplate_callback('user_profile', array('user' => $account, 'fields' => $fields));
  }
?>

There is a typo error. 'account' (wrong) ----> 'user' (correct)

return _phptemplate_callback('user_profile', array('account' => $account, 'fields' => $fields))

Referring http://drupal.org/comment/reply/291436 's snippets, one has to whether to stick with 'user' or 'account'.

If you use check_plain(print_r($profile, 1))

Vayira - June 29, 2009 - 02:54

If you use
print '<pre>'. check_plain(print_r($profile, 1)) .'</pre>';
To find the data

The syntax you need to be able to use one of the listed variables is

print $profile['My data'];

As far as I can tell!

accessing fields

webmayin - January 12, 2010 - 12:06

I'm running 6.15, and have added a profile text field, field_about I am trying to access that field in user-profile.tpl.php and am having trouble.

<?php
print $user_profile;
?>
-- This displays all the fields, including the field_about.
<?php
print '<pre>'. check_plain(print_r($profile, 1)) .'</pre>';
?>
-- This gives me a whole bunch of text, here's a small example:
<span id="thmr_16" class="thmr_call">
  <span id="thmr_15" class="thmr_call">
  <div class="field field-type-text field-field-about">
      <div class="field-label">about:&nbsp;</div>
    <div class="field-items">
            <div class="field-item odd">
                    <span id="thmr_13" class="thmr_call">
  yourabout</span>

I have tried to load the single field with each of the following, from the examples
<?php
print $profile['field_about']['value'];
?>

<?php
print $profile['field_about'];
?>

<?php
profile_load_profile
($user);
echo
$user->field_about;
?>

But nothing shows up. Any help would be appreciated.

I havent tried this but

DrunkMunki - February 3, 2010 - 04:51

I havent tried this but wouldn't it be;

<?php
print $user_profile['field_about'];
?>

Getting started

burtsbees - February 5, 2010 - 02:58

I am using Drupal 6. I am having trouble getting started creating a custom profile. Do I add the user-profile.tpl.php file to \modules\user or \sites\all\themes\acquia_slate? I have it in the themes folder now

This is what I have in my user-profile.tpl.php file as set up on this page:

<?php
print check_plain($account->name)
?>

City:
<?php
print check_plain($account->profile_city)
?>

Country:
<?php
print check_plain($account->profile_country)
?>

Postcode:
<?php
print check_plain($account->profile_postcode)
?>

http://drupal.org/node/35730

I clear my cache, now what is the next step? I am assuming I made a mistake somewhere because I can't see anything different when I bring up my site.

Thanks to anyone who can give me some tips.

 
 

Drupal is a registered trademark of Dries Buytaert.