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 need to create a variable that can be accessed throughout my WordPress Theme template files (index.php, header.php etc..). I know function definitions go inside the functions.php template file (in your theme path), but there's no such thing for variables.

For example I constantly need to retrieve Categories in my Theme, so I would like to have this accessible from anywhere in my theme:

$categories = get_categories(); /* get_categories() is a wordpress function */

This way I can simply access to allocated data, without having to re-allocate it every time I need to get my categories.

Unfortunately adding that piece of code in my functions.php file, doesn't work, neither does making the variable global.

share|improve this question
add comment

migrated from stackoverflow.com Feb 20 '12 at 21:49

This question came from our site for professional and enthusiast programmers.

3 Answers

up vote 4 down vote accepted

Apparently global does the trick. The problem was that my variable $categories should have been redefined with a global in front of it, in each template I needed to use it.

share|improve this answer
1  
Does anyone else know of a more intuitive solution than having to redefine it as global in every template file? I use this method in developing my theme and it looks... out of place. –  Eddie Ringle Nov 9 '09 at 23:04
1  
Awesome dude, thanks! For anyone ready this who's a bit thick like me: In your functions.php file: <?php $test = "I'm a test!"; ?> In your, I don't know, header.php: <?php global $test; echo $test; ?> and you're golden! –  Dominic Nov 30 '10 at 12:04
    
If this helps to clarify: all of the template files in WordPress are called by require() statements which are wrapped in functions: get_header(), get_footer(), etc; which themselves are wrappers around the function get_template_part(). Since functions in PHP only have access to the variables defined within them or passed by reference to them, if you want to use a variable from the global scope (or define a variable and make it accessible in the global scope) you have to announce it with the global keyword first. –  goldenapples Feb 20 '12 at 21:57
add comment

Dominic (don't know how to add a note to his answer):

define only accepts scalars, so you couldn't do define( CATS, get_categories() ); and not even

$categories = get_categories();
define( CATS, $categories );

Otherwise define works fine, and it is in fact safer for scalars (as you can be sure the constants cannot be overwritten)

share|improve this answer
add comment

This also works:

in functions.php add define('TEST', 'this is a test');
and in your header.php or whatever echo TEST;

Is there any perfomance advantage to one method over another?

share|improve this answer
add comment

Your Answer

 
discard

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