This illustrative example shows how easy it is to override theme functions using the User_profile pages as an example.

Before

This is how the out-of-the-box user profile looks like, with extra profile fields, such as City, Country, Postcode, Position etc. added in. (please note that i couldn't fit the whole page into the one screenshot..there is an extra "background/more info." field that doesn't show in the BEFORE screen shot.

click to view the BEFORE screenshot in a new window

After

This is how the exact same user profile looks after overriding the theme and applying a simple user_profile.tpl.php file in my theme directory.

click to view the AFTER screenshot in a new window

How I did it

To override just the layout of the User Profile page..I created a template.php file with this in it:

<?php
/**
* Catch the theme_profile_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));
  }

?>

I uploaded that into my active theme directory and then created and uploaded, to the same directory, the override layout file which is called user_profile.tpl.php.

A very simple/shortened example of how my user_profile.tpl.php works maybe illustrated as follows....(e.g. I have setup custom extended user profile fields called profile_city, profile_country, profile_postcode)....

<?php if($account->picture): ?>
<div class="picture">
<img src="/<?php print $account->picture ?>">
</div>
<?php endif; ?>
/** If you are using this snippet with Drupal version 4.7.x or 5.x use the 
* following line to display a user picture instead
* <?php  if($account->picture) {print theme('user_picture', $account);}?>
*/

<div class="custom_profiles">
<div class="fields">City: <?php print check_plain($account->profile_city); ?></div>
<div class="fields">Country: <?php print check_plain($account->profile_country) ;?></div>
<div class="fields">Postcode: <?php print check_plain($account->profile_postcode); ?></div>
</div>

If you don't want to show empty fields you can use an if check on the field like so:

<?php if($account->profile_postcode) { ?>
<div class="fields">Postcode: <?php print check_plain($account->profile_postcode) ?></div>
<?php }?>

Notes:

Edit your style.css to format the classes.

Security note: I have updated this snippet to include a security check on the content before outputting it, i.e. check_plain(). It's important to remember to check output properly when overriding theme functions in Drupal. (Please consult the How to handle text in a secure fashion for more information).

More details & in depth examples/discussion on this is in the original forum post.

AttachmentSize
before.png14.07 KB
after.png12.85 KB

Comments

benben80’s picture

That s very nice, and a big help. Thank you very much
I m just wondering how it could be to add more pictures...i would like to offer the possibility to put 3 pictures to my users.
Do you know how?
Thanks!

arrow_ben’s picture

Thanks for posting this. It is exactly what I was looking for.

Dublin Drupaller’s picture

the only thing I would add is be extremely careful with the snippets on here. I've noticed some of them have changed since I posted them and some won't work now for the Drupal versions they are tagged with. I don't have time to go through them all, but, just to be wary of using snippets from the handbook.

Also, there are new modules that offer alternatives to using snippets and overrides like this. such as views, panels or the Advanced Profile Kit. Click for a list of profile related modules. I still prefer snippets because it means less modules to install and upgrade (as well as memory demands), but, for newbies, it's worth checking out modules that have been designed to make life easier in that regard.

Dub

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

halloffame’s picture

Heya Dub... Is it safe to use this for Drupal 6? I see that it isn't tagged with D6 but it works perfectly for me. I tried other implementation in handling profile fields but this one seems easiest. I decided to use it but just got wary with the security note. Any advice? Thanks.

Albert-Ros’s picture

здравствуйте, спасибо огромное за статью!!!
хотел узнать один аспект, css файл страницы пользователя помещать в корневую директорию темы? как файл .php или в папку css темы ?

hello, thank you very much for the article !!!
wanted to know one aspect, place the css file of the user page in the root directory of the theme? as the .php file or in the css folder of the theme?