Global Variables

Any suggestions in this regard?

I know that some devs don’t like using global variables as a general rule, but what alternatives are there for easy maintenance and consistency for things like company contact information and such?
Especially when you have the business owners in charge of the content?
I think this is the sort of thing GB tries to address with re-usable blocks, but there must be another more efficient way?

Any alternatives?


What is the context?
WordPress global variables? Plugin? Theme?
Site content should be in the database.

I use 1 global array with a long randomized name to keep everything and contol it through a getter/setter function. And public data is available for content-managers via shortcodes.

1 Like

I was thinking of having a general discussion, but sure, will give a very specific example as a starting point :slight_smile:

Let’s say you have the business telephone number that you display in a number of places, but you don’t want to “hard code” it into the various places it may appear. Footer, widgets, posts, contact pages, etc.
Most organizations will probably have a number of these sorts of items.

I was also considering entities who may wish to apply consistent branding to their names / taglines, etc. (Edit: For example, it HAS to be in green / bolded / underlined / font otherwise formatted in a very specific way.)
So, one may want to provide the option to update them in one place.

As there may be quite a number of these on one post / page display, it would be nice if each one does not require a separate query of the db?
(I think Norske’s solution would achieve that? I also think that short-codes are the most user-friendly solution for content managers with limited technical skills? Has anyone encountered another user-friendly option?)

A plugin for snippets or “custom shortcodes” is pretty simple. These things should not be in the theme (although I can see it in a child theme specifically for the site). So a plugin provides a way to store it in the database and a way to output it where desired, checking for the correct user capability along the way.

I think I’d either store such a thing as an option, going in the options table of the database which is very speedy to access, or else store it as postmeta and then use a transient to be able to retrieve it.


Since I use Advanced Custom Fields on all websites I develop, I always handle those kinds of ‘global’ content needs with ACF Options.


Technically my sites have a “branded” section of custom options in Admin menu right below the Dashboard. Option pages count and tabs count depends on a project structure. I try to limit 5-7 options for a single tab. There goes everything that is related to Client and is useful for content-managers. I even make short tutorial subpages sometimes — it’s not a proper place, probably, but it works much better there than anywhere else for some types of my users.

Most of input fields are related to special shotcodes. Some fields like phones are used by several shortcodes displaying different formats (human-readable, tel attribute, regional code nuances, iconic etc). But certainly all input is filtered, escaped etc.

Common options are loaded into that global array mentioned above. I initialize all needed custom fields and options only once, early, before outputting header. All values are stored in array and called via getter function, so each template can access them if needed.

I avoid updating option(s) from public-end, so all external user input (orders, messages, form submits) pass through another procedure with more strict validation and sometimes redirection to an external CRM by API without saving copies on site.

And I also use ACF, but for content pages only. Option pages allow to reorganize layouts for better usability. But if I make a kind of “private section” outside /wp-admin/ (rarely) ACF is pretty heplful.

Update: just notices that @raygulick wrote about ACT Options Page — I didn’t use it yet, since I have custom solution, but probably it’s worth trying. ACF plugins a very nice.


This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.