WordPress.org

Ready to get started?Download WordPress

Forums

WP destroys header's global vars? (7 posts)

  1. Blutarsky
    Member
    Posted 4 years ago #

    I'm using WP 2.9.2 and I cant' understand why global vars defined into header.php are destroyed, once they're accessed from other .php modules, like functions.php.

    Can someone help?

  2. Blutarsky
    Member
    Posted 4 years ago #

    While trying to debug out the problem.... I have noticed that I'm loading a .php module into the sidebar using ajax asynchronous load; that it is, to achieve this I need to preload into the .php module the following call:

    <?php require_once( '../../../wp-load.php' );

    otherwise the ajax loaded module will not "see" WP environment.

    Could this re-initialize global vars?

  3. Blutarsky
    Member
    Posted 4 years ago #

    Keeping testing.

    Even before the 'require_once' statement there's a strange behavior:
    - declare a variable either in functions.php or in header.php OUTSIDE of a function (PHP expects this var to be global)
    - in header.php the variable is visible (has proper value)
    - in index.php the variable is destroyed
    - in a function (using the 'global' notation) the variable is destroyed

    Can someone help?

  4. Blutarsky
    Member
    Posted 4 years ago #

    WP shouldn't destroy vars according to PHP manual:

    The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope. This single scope spans included and required files as well. For example:

  5. Blutarsky
    Member
    Posted 4 years ago #

    I have just read this.....

    ....
    WordPress doesn't use standard PHP includes - so variables defined outside of a function, that is itself called within a template file, won't work.
    ....

    What a shame!! It should be written with capital letters somewhere!

  6. gregboutin2
    Member
    Posted 3 years ago #

    Completely agree with you, Blutarsky. It doesn't make sense that one cannot define variable in the header file for all subsequent template pages to use.

    I had to take time to figure out why it wouldn't work. And then repeat each variable calculation in each template as a result.

  7. gregboutin2
    Member
    Posted 3 years ago #

    actually if you specifically declare them as global they work across other pages. just tested and it works. it's weird i though that the global function was to let outside variable into a require or function, not the other way around.

Topic Closed

This topic has been closed to new replies.

About this Topic