My global variables are not being very global :)

Hi there, I am trying to use global variables for the first time, but they are behaving more like local variables. I have used local variables extensively and never had a problem.

If I set the variable in the Document Head location, they only show up when called in the Footer. They also work within the same template like a local variable.

Why might a global variable set in the Document Head location not be available to a template in the Content location? Thanks.

Example code:

<Set name="sim_post_archives">
    <Loop acf_taxonomy="sim_post_archives" from="options">
      <Field title />

<Get name="sim_post_archives" />

I believe this is happening because the template for the theme Content position is actually rendered before Document Head, then later its result is placed in the <body>. This is by design, so the template’s styles and meta tags can be loaded in document <head>.

You can confirm if this is the case, by setting a variable in the Content and getting it from the Head.

This is probably unintuitive - but the loading logic is not easy (or maybe not possible) to change, due to the way Document Head is loaded in action wp_head, after the Content is rendered already.

I’m not sure if there’s a good way to work around this. Maybe a shared template loaded by Content and Head position.

1 Like

Thanks for the info. I just assumed that globals would be truly global, but with the Content position, they need to be set at the start of the template in that position.

Similar to your suggestion, I created a template to store all my site-wide data globals, and I’m calling this at the start of each template that loads in the Content position. This at least keeps things tidy by centralising all the globals to a single template. :slight_smile: