UPDATE: My original question has been solved, but this is turning into a valid discussion about why not to use global variables, so I am updating the question to reflect that. The solution was <?php global $category_link_prop; echo esc_url( $category_link_prop ); ?>
as @TomJNowell suggested.
UPDATE 2: I now have it doing exactly what I wanted. But I’m still using global scope and would be happy to find a better way.
I am trying to set up a whole bunch of global variables for the permalinks to categories to be used in various places in my theme. The main reason for this is for use in both the main navigation, as well as in a series of sub navigations that are chosen based on what category the current post is in. This is not a theme I will be releasing for use by others, but is built for one very specific purpose.
This is how I am currently creating them (I’ve only pasted in a few of the variables).
function set_global_nav_var()
{
//proposal
global $prop;
// Get the ID of a given category
$category_id_prop = get_cat_ID( 'proposal' );
// Get the URL of this category
$category_link_prop = get_category_link( $category_id_prop );
$prop = '<a href="' .esc_url( $category_link_prop ). '" title="Proposal">Proposal</a>';
//Calvinball
global $cb;
// Get the ID of a given category
$category_id_cb = get_cat_ID( 'calvinball' );
// Get the URL of this category
$category_link_cb = get_category_link( $category_id_cb );
$cb = '<a href="' .esc_url( $category_link_cb). '" title="Calvinball">Calvinball</a>';
}
add_action( 'init', 'set_global_nav_var' );
I can now do <?php global $prop; echo $prop; ?>
int he 4 places that goes and get back the whole link for the code. When that changes I only need to change it in one place. I’m open to alternatives that do not involve the global scope.
While I strongly advise against this, and it will not speed things up, your usage is incorrect.
WordPress already caches these things in the object cache/memory so it doesn’t have to fetch it multiple times in the same request, you don’t need to store the result and reuse, WP does that already out of the box.
It’s very likely your code is running slower as a result of this micro-optimisation, not faster!
How To Use Globals
When you try to use a global you must specify the
global
keyword first. You have specified it here when defining its value, but outside of that scope it needs to be redeclared as a global scope variable.e.g. in
functions.php
:In
single.php
, this will not work:Because
$hello
is undefined. This however will work:Of course you should do neither. WordPress already attempts to cache these things in the object cache.
Disadvantages and Dangers of Global Variables
You will see no speed increase from doing this ( you may see a tiny speed decrease ), all you will get is additional complexity and the need to type out a lot of global declarations that aren’t necessary.
You’ll also encounter other issues:
global
What Should You Use Instead?
You would be better off using structured data, such as objects or dependency injection, or in your case, a set of function.
Static Variables
Static variables aren’t good, but think of them as the slightly less evil cousin of global variables. Static variables are to global variables, what mud covered bread is to cyanide.
For example, here is a means of doing something similar via static variables e.g.
Singletons
Singletons are like static variables, except the class contains a static variable with an instance of that class. They’re just as bad as global variables, just with different syntax. Avoid them.
WP_Cache, The Thing You Tried to Do But WP Already Does It
If you really want to save time by storing data somewhere to re-use, consider using the
WP_Cache
system withwp_cache_get
etc e.g.Now the value will get cached for the life of the request by WordPress, show up in debugging tools, and if you have an object cache it’ll persist across requests
Sidenote 1: I would note, that some people try to persist data in global variables across requests, unaware that this is not how PHP works. Unlike a Node application, each request loads a fresh copy of the application, which then dies when the request is completed. For this reason global variables set on one request do not survive to the next request
Sidenote 2: Judging from the updated question, your global variables give you no performance gain at all. You should just generate the HTML as and when you need it and it would run just as fast, perhaps even a tiny bit faster. This is micro-optimisation.
Don’t use global variables, as simple as that.
Why not to use globals
Because the use of globals makes it harder to maintain the software in the long term.
WordPress core has way way way much to much use of globals. While trying to understand how basic functions and hooks like
the_content
work, you suddenly realize that the$more
variable is not local but global and need to search whole of the core files to understand when is it set to true.So what can be done when trying to stop copy-pasting several lines of code instead of storing the first run result in a global? There are several approaches, functional and OOP.
The sweetener function. It is simply a wrapper/macro for saving the copy/paste
The benefits are that now there is a documentation to what the former global does, and you have an obvious point for debugging when the value being returned is not the one you expect.
Once you have a sweetener it is easy to cache the result if needed (do it only if you discover that this function takes a long time to execute)
This gives you the same behavior of a global but with the advantage of having an assured initialization every time you access it.
You can have similar patterns with OOP. I find that OOP usually doesn’t add any value in plugins and themes, but this is a different discussion
This is a clumsier code, but if you have several values that you would like to precompute because they are always being used, this can be a way to go. Basically this is an object that contain all of your globals in an organized way. To avoid making an instance of this object a global (you want only one instance otherwise you recompute the values) you might want to use a singleton pattern (some people argue it is a bad idea, YMMV)
I don’t like to access an object attribute directly, so in my code it will wrap some more
Your question is involved with how php works.
Take $wpdb as example
$wpdb is a well-known global variable.
Do you know when it’ll be declared and assigned with values ?
Every page loaded, yep, every time you visit your wordpress site.
Similarly, you need to make sure those variables that you want to be globalized will be declared and assigned with corresponding values every page loaded.
Although I’m not a theme designer, I can tell the after_setup_theme is one time hook. it’ll only be triggered when theme activated.
If I were you, I’ll use init or other hooks. No, if I were you, I won’t use global variables at all…
I’m really not good at explaining things. So, you should pick up a book if you want to delve into PHP.
You can always use a singleton pattern via static getters.