WordPress.org

Ready to get started?Download WordPress

Forums

Best way to have glabal variables (4 posts)

  1. Zoom123
    Member
    Posted 7 years ago #

    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?

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    ...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.

  3. Zoom123
    Member
    Posted 7 years ago #

    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.

  4. pizdin_dim
    Member
    Posted 7 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic