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.

  • Won't make this an answer as I'm not sure but I think the index.php should be right...
    – dpmguise
    Nov 26, 2010 at 3:56

3 Answers 3


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.


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.


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.


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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