Support » Themes and Templates » get_sidebar() – How Does It Work?

  • Resolved Cory Dee


    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?


Viewing 5 replies - 1 through 5 (of 5 total)
  • 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;

    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,

    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.

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

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

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘get_sidebar() – How Does It Work?’ is closed to new replies.