(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 ryze.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>'; ?>
<p>You can also use Devel dsm() function to see drill-down view of available variables to your custom <strong>user-profile.tpl.php</strong>.</p>
<?php dsm($profile); ?>
</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($account); 
// now you can call the profile field like profile_firstname
echo $account->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.

Comments

liquidcms’s picture

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:

  if (module_exists('imagecache_profiles'))  {
    print phptemplate_user_picture($user);
  }

to your tpl file.

Peter Lindstrom
LiquidCMS - Content Management Solution Experts

zanforlin’s picture

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));
  }
?>
jessicakoh’s picture

Below is the correct one.

/**
* 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'.

Vayira’s picture

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!

arne.olafson’s picture

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.
print $user_profile; -- This displays all the fields, including the field_about.
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
print $profile['field_about']['value'];
print $profile['field_about'];

 profile_load_profile($user);
echo $user->field_about;

But nothing shows up. Any help would be appreciated.

DrunkMunki’s picture

I havent tried this but wouldn't it be;

print $user_profile['field_about'];
burtsbees’s picture

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:

print check_plain($account->name)
City: print check_plain($account->profile_city)
Country: print check_plain($account->profile_country)
Postcode: 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.

arobq’s picture

Hi Burtsbee,

I believe the proper location is in you theme folder, in order for you to see the changes made to the file you created in your selected theme folder, you must first get rid of the user-profile.tpl.php located here "your-root/modules/user/user-profile.tpl.php" . With this solution you just have to remember to get rid of the user-profile.tpl.php file after every time you update your site. If there is a better way, let me know.

arobq

el_reverend’s picture

I believe that the active theme overrides the available template files of core or other modules and you should never have to remove anything.
http://api.drupal.org/api/group/themeable/6

maxudit007’s picture

Hello,

I am using drupal 5.x, i want to show email field accessible to all users as at the moment it is only accessible to only administrators, on profile page. can someone help?

Email is only showing to administrators , not to normal members.

Thanks in advance.

Dan Silver’s picture

I made a video tutorial. Check it out at http://www.youtube.com/watch?v=5UePH2E9QfA

Sepero’s picture

That video link is broken

webservant316’s picture

First I created myzensubtheme/user-profile.tpl.php thus...

    print '<div class="profile">' . $account->profile_firstname . '</div>';
    print '<div class="profile">' . $newvariable . '</div>';
    

Second I created the user profile preprocessor override function in myzensubtheme/template.php thus...

    function myzensubtheme_preprocess_user_profile(&$vars, $hook) {
        $vars['newvariable'] = 'hello world';
    }
    

Third I added the override function to the list of override hooks in myzensubtheme/template.php thus...

    function myzensubtheme_theme(&$existing, $type, $theme, $path) {
        $hooks = zen_theme($existing, $type, $theme, $path);
        $hooks['user_profile'] = array('template' => 'user-profile', 'arguments' => array('form' => NULL));
        return $hooks;
    }
    

Now the weird part, when the hook is enabled the $newvariable prints, but the $account->profile_firstname does not (why?).
And the the hook is disabled the $account->profile_firstname prints but the $newvariable does not (as one would expect).

Thanks for any help.

Anyone who gives another a cold cup of water certainly will not lose his reward!

rjperry’s picture

I do not know PHP and i have been using snippets to custom theme my user profile pages. I use the following code:

			profile_load_profile($account);

			echo $account->profile_birthday;

It displays "Array" instead of the date of the birthday field.

when i use this:

			profile_load_profile($account);
			// This is the email address section
			echo $account->profile_email;

it displays the email field fine.

How do i get my fields that have dates to display that info and not "array"?

rjperry’s picture

print date("F j, Y", mktime(0,0,0,$account->profile_hiredate["month"],$account->profile_hiredate["day"],$account->profile_hiredate["year"]));

is what i used to get time to show.

I used:

print_r($account->profile_hiredate);

That returned this: Array ( [month] => 3 [day] => 1 [year] => 2011 )

I then had to convert the function mktime() to display:

March 1, 2011

http://php.net/manual/en/function.date.php gave me formatting options.

Thanks to Emma Jane Hogbin for her help and explanation.
http://sitebuildingextravaganza.com/about-instructor

thejamesjones’s picture

Can anybody tell me where the "contact person by email" variable is found. It is one the user page, I am creating a custom user page and need to print it.