Skip to main content
Tweeted twitter.com/#!/StackWordPress/status/194609152708968448
edited tags; edited title
Link
onetrickpony
  • 13.6k
  • 7
  • 59
  • 87

functions.php global variable alternatives? Passing variables to templates (alternatives to globalizing variables)

Source Link
Wordpressor
  • 5.1k
  • 19
  • 70
  • 102

functions.php global variable alternatives?

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?