最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - WordPress Theme variables scope

matteradmin8PV0评论

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.

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 edited Oct 19, 2018 at 3:05 Gonçalo Peres 1171 gold badge1 silver badge11 bronze badges asked Nov 6, 2009 at 14:16 Luca MatteisLuca Matteis
Add a comment  | 

4 Answers 4

Reset to default 5

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.

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)

I know this one is really old, but there is a room for improvement.

You should consider using $GLOBALS['categories'] instead of just global.

There are two reasons for this:

  1. We don't have to write global $categories; everytime.
  2. It's crystal clear then we are using global and then not.

Consider this code:

global $categories;

// a lot of PHP code here

<?php print_r ($categories) ?>

Only if we initialize global state right before using variable, it's pretty hard to tell, if it's global or not. And don't forget to repeat it in any of template files you have.

It's possible to use naming conventions for that, but there is a better way, in my opinion.

Consider using $GLOBALS['categories'].

We only have to initialize our variable one time in functions.php without having to think about global $categories again. And we can see it's a global one.

print_r ($GLOBALS['categories']);

Performance issue is not a an issue at all in this situation. I'll quote Sara Golemon (link):

What does that mean for your use of the $GLOBALS array? That's right, the global keyword is technically faster. Now, I want to be really clear about one thing here. The minor speed affordance given by using your globals as localized [compiled variables] needs to be seriously weighed against the maintainability of looking at your code in five years and knowing that $foo came from the global scope. something_using($GLOBALS['foo']); will ALWAYS be clearer to you down the line than global $foo; /* buncha code */ something_using($foo); Don't be penny-wise and pound foolish..

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?

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far