• Resolved darkryder

    (@darkryder)


    I’m having problems with variable scoping in templates and wasn’t able to find a solution in the Codex or the forum here.

    I’m trying to find a way — any way — to share a variable between template files. I use a separate file to store a few simple settings for my template (what to show in the sidebar, for example). Because these are used across multiple templates (index.php, single.php, …), I am include_once()ing my options file in header.php. Functions defined this way work fine, but variables are always NULL in other files.

    I have tried using global $xyz; in both files and even referencing $GLOBALS['xyz'] directly, but no luck.

    Has anyone run into this problem before?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Not sure why scoping the vars to global isn’t working, but one thing you can try is to set up a functions.php file with your theme (if one doesn’t already exist) and use that as your ‘options file’ instead of the one you include in your header.php.

    A theme’s functions.php in included automatically by WordPress, and is useful for theme-specific functions, etc.

    Thread Starter darkryder

    (@darkryder)

    Thank you so much! Not only is this a handy trick, but in the process of taking my three files (functions.inc.php, defaults.inc.php, and options.inc.php) and turning them into a single file, I found the source of the problem.

    You know what makes your include()ed files work better? Putting <?php at the beginning…

    >_<

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Variable scope across template files’ is closed to new replies.