Hello,

I would like to setup a different background image for each user. (My site only has 10 users max.)

I am not sure how to accomplish this. I could duplicate my theme 10 times and assign one theme per user, but that seems that a waste of resources when all I need is a custom background image.

Any suggestions/ideas?

Thanks!!

Comments

nevets’s picture

You could add a class to the body tag that represents the user, say something along the line 'user-1', 'user-2', etc and then use the class to apply an unique background image.

NToronto’s picture

Hello Nevets,

I could add a class to the body tag that represents each user as per your suggestion, but how do I then ensure that the class 'user-dave' only shows up when Dave logs in?

It sounds like i need CSS tokens (background-image: [username].jpg) etc, but that is not possible...

nevets’s picture

It depends a bit on your theme, if you look at your themes page.tpl.php what does the opening body tag look like?

jhl.verona’s picture

You can simply add a print 'style="background-url: path/to/images/user-'. $user->uid .'png;"' to the <body> element. You'll need a user-0.png for anonymous users. The remaining CSS properties can be put in a class.

I wouldn't use the $user->name because you could run into encoding problems: François Di Andrè for example.

Also remember to import the global variable with global user; first...

NToronto’s picture

Thanks jhl.verona for the suggestion.

Could you clarify, where does global user; need to be placed? Can it be placed directly above the body tag?

jhl.verona’s picture

It can go anywhere, as long as it is before you reference it. I'd put it close to where it's being referenced.
Something like the first example in http://drupal.org/node/17272
Of course, if you really want to do things 'the right way', then you could follow the guildelines in http://drupal.org/node/92657
The second example would make sense if you predict having more than one page.tpl.php template, perhaps you'll end up with a page-front.tpl.php and a few page-[node-type].tpl.php. That way the code that does the work is in one place - the function, and not repeated in n templates.

mooffie’s picture

A couple of years ago I answered a somewhat similar question:

http://drupal.org/node/263901

(But the color there is associated with the node's author, not with the user viewing the page. But at least you can pick the profile field trick.)

I could duplicate my theme 10 times and assign one theme per user, but that seems that a waste of resources

The waste is negligible.

You don't need to "duplicate the theme": a new theme will consist of just an ".info" file pointing to the original theme ("base theme = whatever"). Then, for example, make the original theme add the theme's name to the BODY's class. Or add a minimal CSS file for each sub-theme.