Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a php file with some variables which I would like to use.

When I include it in the header.php the variables in that file are not recognized at footer.php and some other places.

Where is the best place to include this file so its content will be shared all over the wp files.

share|improve this question
    
Won't make this an answer as I'm not sure but I think the index.php should be right... –  dpmguise Nov 26 '10 at 3:56

3 Answers 3

up vote 4 down vote accepted

Variables have a certain scope. The PHP Manual explains that in detail. So when you set a variable you should know in which scope those are set. This depends on where you set them and how that file gets included.

As Rarst already suggested, the function.php file is an ideal place as it gets included on the global space whenever your theme is active.

Next to that, scope still applies. The footer.php file for example normally is not included on the global scope. To access your variables therein - if you have set them globally - you can refer to the $GLOBALS superglobal array.

This normally does it for some variables. If you have multiple, you might consider to attach all your variables to an array instead, so you have only one variable name in the global scope you need to refer to. This keeps things a bit more apart from each other which makes it easier in the long run. Because if you name your variables the same as existing variables, you will overwrite them. That can break things that are hard to debug.

Example:

in function.php

$mytheme_config = array();
$mytheme_config['extra_footer_display'] = true;

in footer.php

if ($GLOBALS['mytheme_config']['extra_footer_display']) {
    // executed when extra_footer_display is true
}

This is just a very basic example, but it probably already does the job for you. I don't know your level of experience with PHP, but as you're starting probably, the links provided above you give you the basic understanding how this is working. Just keep in mind, that template parts are not loaded within the global scope so you need to reference global variables with the $GLOBALS superglobal array to access them. $GLOBALS is always referring to global variables regardless of the scope where it is accessed.

share|improve this answer
    
Thank you for the detailed answer –  Ofer Nov 30 '10 at 4:13

The best place to include code in theme is usually Functions File (functions.php) rather than template files (which can and often are overridden by plugins, child themes, etc).

Depending on your specifics you can define constants in there or create wrapper function and hook it to some action firing at later stages of theme load.

Also depending on specifics you might want to look into Options API to store values in database.

share|improve this answer

You need to make the variables constants and include them in header.php Using index.php will only include them on your homepage/main blog page, depending on your settings and will miss any category, tag, search, and page.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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