WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] get_sidebar() - How Does It Work? (6 posts)

  1. Cory Dee
    Member
    Posted 6 years ago #

    How exactly does get_sidebar() work? I had (incorrectly) assumed that the function worked similarly to an include function, however, I am finding variables defined in my header file aren't available in the sidebar being called.

    Any ideas what could be going on?

    Thanks,
    Cory

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

    get_sidebar does work similarly to an include, but not entirely. Because it goes into a function for this, the variables in the header or sidebars and such are in different scopes. This is intentional, actually.

    So, if you want to use stuff from the header in the sidebar (same for the main page, the footer, etc), then you need to define them as global variables just before you use them.

    global $var;
    $var = whatever;
  3. Cory Dee
    Member
    Posted 6 years ago #

    Thanks for the prompt reply.

    Having to declare the variable as global isn't what I wanted to hear, but this instance is more of a constant anyway, so it won't be a huge hit to take. (For the record, it is just a $root var).

    Thanks again,
    Cory

  4. Cory Dee
    Member
    Posted 6 years ago #

    Just for an update, the target server has register_globals set to off. My final solution was to set a constant.

    Mileage may vary depending on your variables and intentions.

  5. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    ... register_globals doesn't really have anything to do with manually declaring a variable global. register_globals controls whether or not incoming GET and POST values are automatically turned into global variables. In fact, register_globals should be off at all times, for security purposes.

  6. Cory Dee
    Member
    Posted 6 years ago #

    Than maybe I associated the symptoms with a different disease...Either way it is corrected now.

Topic Closed

This topic has been closed to new replies.

About this Topic