Where to add every-page code?

thrice - January 18, 2006 - 14:58

I need to add some PHP code to each and every page of my website. I've identified my phptemplate-based theme code to be the best place to insert that. Are there any other possibilites? I've always associated themes with colours and images, not site specific code, so I'm having trouble coming to terms with the idea that this is the place for my code. :o)

you mean the page.tpl.php

BlackSash - January 18, 2006 - 15:27

If you mean that you want to put some code on every page (say you want to add some footer text or something, you'll have to edit your page.tpl.php.
I usually put this in http://your.server.domain/sites/default/themes/custom/
but other locations could be the themes (http://your.server.domain/themes/name_of_theme) directory... you'll have to search your drupal install for the actual file. If you want to put something in every node, search for a node.tpl.php file.

Yeah, the theme

dman - January 18, 2006 - 15:32

if the 'code' results in something being displayed in the browser, the theme is a place to start.

Depends entirely on what the code is as to where it should live.

If you wanted, you could put code in settings.php (if it's site-specific, like you say)

If, OTOH it's functional, you could make and enable a bare-bones module that impliments almost no hooks at all - it'll be included on every page, so it'll run anything you code inline (not in a function) for every page.

What does the code DO?

When you you WANT IT to run?

.dan.

http://www.coders.co.nz/

This isn't display code

thrice - January 18, 2006 - 15:46

The code in question will check the existence of a cookie and the information in that cookie, then set some session information and possibly some application database information. It might then cause a redirect to another page. It won't display anything, it's purely business logic for the application itself.

I want it to run at the very start of the page request. This is the code that decides whether the user is asking for information they are entitled to, and if they are entitled to see it, whether the format they've requested it in is appropriate given their usage experience, etc. The data from the DB might get trimmed or reformatted on the basis of this decision making.

Ideally I'd like to be able to use this code from any theme or website, so keeping it isolated from everything else would be my favoured approach.

Now why was I thinking authentication...

dman - January 18, 2006 - 16:24

Authentication checking is the only time I'd really see it going in the settings.php

But you still have time to do a redirect if you put it in a mini module.
I do modules for just about every unique feature or client. It's so handy to be able to just selectively turn them off when concentrating on one thing. Coding that way stops your dependancies from getting messy too.

You only have to declare a hook_help so it'll show up in the menu. The rest you can execute at include time.

.dan.

http://www.coders.co.nz/

Where to add every-page code?

sevo - January 18, 2006 - 17:28

Themes consist of both of a wrapper or wrappers that build pages (respectively page sub-elements) from the individual components, and style sheets to give them a visual representation.

If you want to put something on all pages, and want to maintain compatibility with generic themes, you could make it a block, or, if you don't want sidebar placement, handle it with the flexiblock module, which allows for free block placement.

But if you want your code to be outside the block/node display hierarchy or superimposed on the pages (as in the case of e.g. an access control wrapper with legal preamble display), the outer wrapper script of your theme is the best place to put it, as that is the only place where content can be placed outside of and separate from any element (node, block, logo, links etc.).

regards Sevo

 
 

Drupal is a registered trademark of Dries Buytaert.