WordPress.org

Support

Support » How-To and Troubleshooting » Best way to have glabal variables

Best way to have glabal variables

  • I find it a bit annoying that if I want a variable from the header to be accessed from another part of the template (e.g. sidebar) that the variable has to be declared as global in both locations. I am integrating another script into wordpress and I have 10s of such variables.

    Isn’t there a better way to do this?

Viewing 3 replies - 1 through 3 (of 3 total)
  • …if I want a variable from the header to be accessed from another part of the template (e.g. sidebar) that the variable has to be declared as global in both locations…

    Yes. That’s how PHP works.

    Isn’t there a better way to do this?

    Depends on what it is, exactly, that you’re trying to do. If it was me, I would attempt to put my code into a plugin and see if I could do it without template changes at all. But that may or may not be feasible, I cannot say without a lot more information on what you’re trying to do.

    Yes. That’s how PHP works.

    With PHP If you just use “include” to include header, sidebar etc, varibales declared in an include earlier on will be available to code in includes that follow it.

    I have tried already to put all code in plug-ins but the problem is the same.

    Well, it’s not really a problem, is it? It’s just the way the scripting language works. An alternate way is to reference everything through the super-globals $GLOBALS array. See this for more.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Best way to have glabal variables’ is closed to new replies.
Skip to toolbar