I am looking to load a field from a user's profile that is being viewed.For example, I am looking at another users profile and it will load their associated field.

I am programmatically adding a picture to the tpl (probably should be template.php) and then I am adding it to the background-image of a div. My code loads the user but then switches the user to user being viewed. It is obviously not good to be able to switch users.

Here is my code:

   $user = user_load(arg(1));
   $picture = field_get_items('user', $user, 'field_picture');
   $file = $picture['filename'];
   $url = $base_url . '/files/' . $file;

I cannot find a secure way to do this. Anyone have an idea?

Thank you for your time.


Try creating a user-profile.tpl.php file in your theme and messing around with that.

Default contents are:

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

And I suggest using a variable name other than $user as $user is a global variable referring to the logged in user.

  • I have two custom pages to process profiles. One for the logged on user and another for the user to view other profiles. Each one queries a little different things. I maybe could look into merging the two. This code is all in my page--user--%.tpl.php file. $user-profile isn't loading anything in there when I print it. – Bestinc Feb 21 '15 at 2:49
  • Ah right, so you need to load variables for your custom pages in a preprocess function. – Darvanen Feb 21 '15 at 4:23
  • It actually seems to be working by changing the name of the $user variable. Just one question before closing this up, is there any security implications I should consider with this? – Bestinc Feb 21 '15 at 16:17
  • Really depends on what you are trying to do. If the pages are forms or they show personal details of users you probably want to set an access_callback which uses either a pre-existing permission or a new one you create using hook_permission. Other than that, what are you worried might happen? – Darvanen Feb 22 '15 at 1:01
  • I am just spooked from the fact the code was automatically switching user when you visited their page. Now I am paranoid haha – Bestinc Feb 22 '15 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.