WordPress.org

Ready to get started?Download WordPress

Forums

passing value through get_header to header.php (9 posts)

  1. holsclaw
    Member
    Posted 3 years ago #

    I am calling get_header() and would like to pass data to header.php for use during processing. Any suggestions?

  2. Mitchell Bundy
    Member
    Posted 3 years ago #

    the simple route: use global $var; before declaring your variables, and before calling get_header()

  3. holsclaw
    Member
    Posted 3 years ago #

    Thanks for the reply!
    I have tried that and it does work. However, I have multiple pages that need to pass the data (title). I am concerned that multiple, simultaneous page requests might cause the wrong value to be displayed.
    Is there a 'safer' way to do this that would tie the data to the page or request?
    Thanks again,
    Dave

  4. Mitchell Bundy
    Member
    Posted 3 years ago #

    what do you mean by simultaneous page requests?

    There are definitely better ways to do this. Hooking into wp_title and passing whatever you need into there would be the best way to go about things.

  5. holsclaw
    Member
    Posted 3 years ago #

    >re simultaneous page requests
    Two different users access the site. page_one.php sets the $my_title value to "PageOne". Then before the header.php code is executed for page one, page_two.php sets the value to "PageTwo". Since it is a global variable, and assuming that there could be multiple threads generating WordPress content, isn't it possible that both pages would be displayed with the title "PageTwo"?
    >re: wp_title
    I'll look into that again. Since header.php makes that call, every approach that I've come up with requires passing of some kind of data/flag.
    Thanks again.

  6. Mitchell Bundy
    Member
    Posted 3 years ago #

    Nope, that's impossible. global variables are only global within the script. It will never happen. I'd read up on variable scope:
    http://php.net/manual/en/language.variables.scope.php

  7. holsclaw
    Member
    Posted 3 years ago #

    I really appreciate the help and have read that document.
    Do I understand you that page_one.php and page_two.php won't collide even if they use the same variable name ($my_title) and both call get_header() to load header.php?
    So is each page load handled by a separate php instance? That seems expensive but I guess it is good news in this case.

  8. holsclaw
    Member
    Posted 3 years ago #

    I did some testing of multiple pages and it does appear that global variables aren't shared across pages. Thanks Mitchell for your help.

  9. shaguna
    Member
    Posted 2 years ago #

    Hi holsclaw,

    I'd like do something similar, but am all confused with the scope of global variables. Could you pls share your code snippet?

    Here's what i'm doing:
    1. I'm declaring a global var in my index.php, like this:

    <?php
    global $login_redirect_to; //// declare global var to store calling page
    $login_redirect_to='blank';
    ?>

    2. Then, trying to use it in my header.php, but am unable the fetch the value set in index.php

    global $login_redirect_to;
    echo 'a. Inside Header, value of login_redirect_to = ' . $login_redirect_to;
    $login_redirect_to = get_permalink();
    echo 'b. Inside Header, after setting, $login_redirect_to = ' . $login_redirect_to;

    I need to reset this value (in header.php) and use further, but i'm stuck at the first step.

    Thanks,
    Shaguna

Topic Closed

This topic has been closed to new replies.

About this Topic