WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to make my WP code simpler (there's some mess within header.php, page.php etc.). So, for example, I want to set something like this global:

$foo = get_option('foo');  

So $foo will work properly used anywhere in page.php, header.php, footer.php, loop.php and also within different theme files linked to functions.php (require_once).

I already know two ways of achieving this, both require editing functions.php:

Global dynamic function

function foo() {
   return 'bar';

Global constant

define('FOO_BAR', 'directory');    

Do I really need to fire a function in multiple places just for one variable? Isn't it an overkill?

share|improve this question
Isn't global $foo; $foo = 'bar'; and then in your template echo $foo; sufficient enough? – userabuser Apr 24 '12 at 0:08
up vote 7 down vote accepted

You could also use this function:

set_query_var('foo', $foo);

WP will extract and expose all query variables in every template it loads, so you will be able to access it as $foo

share|improve this answer
This is brilliant! :) – Wordpressor Apr 23 '12 at 23:18

Your Answer


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.