• I’ve confirmed this on multiple blogs of my own and also a friend tested it out on his 2.7 install:

    global variables are not preserving through to the footer.php

    In my case, I have the following code:

    header.php
    <?php global $cat39;?>

    single.php
    <?php if(in_category('39') ){ $cat39=1; }?>
    <?php echo "test: " . $cat39;?>

    footer.php
    <?php echo "test: " . $cat39;?>

    Now, I can confirm that in single.php the echoed value of $cat39 is 1. However, by the time you get to footer.php the echoed value is blank (empty, nothing).

Viewing 5 replies - 1 through 5 (of 5 total)
  • As far as I know, the global assignment keyword can only be used within a function. Please correct me if I am wrong.

    Thread Starter ryancaldwell

    (@ryancaldwell)

    ok. anyway, if I remove the global assignment, the problem persists -> I can’t access a variable defined in single.php in footer.php

    Alright, I figured this out after fiddling with it for hours. Also learned something new about php. Documentation on php.net was non-existent or I didn’t find it.

    Here’s how you do it.
    In the header you declare the variable with the GLOBAL keyword (just like you thought).

    But, in the footer you must do the same thing. First, declare the variable as GLOBAL again. Then you can echo it.

    So …
    in the header file

    global $my_variable;
    $my_variable = "some arbitrary value";

    Then …
    in the footer file

    global $my_variable;
    echo $my_variable;

    And …
    Your footer will display “some arbitrary value”

    Ed N.

    (@enailor)

    I have seen another reference to this solution that ody3307 refers to, so obviously this works. However, there is a further problem here.

    WordPress functions also get emptied in the footer.php section of a page. For example, I can not call is_front_page() in the footer. I had read that this might be a one time call (which made no sense) but I have called it in the header and again in the page and both have worked. But if called in the footer, it is ignored.

    So while I can re-global variables, how am I to get the wordpress functions valid again?

    I believe you can solve this by using a PHP include instead of get_footer.

    use

    include ‘footer.php’;

    instead of

    get_footer()

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘WordPress 2.7 Emptying Global Variables Before Footer’ is closed to new replies.