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

Viewing 8 replies - 1 through 8 (of 8 total)
  • the simple route: use global $var; before declaring your variables, and before calling get_header()

    Thread Starter holsclaw

    (@holsclaw)

    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

    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.

    Thread Starter holsclaw

    (@holsclaw)

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

    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

    Thread Starter holsclaw

    (@holsclaw)

    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.

    Thread Starter holsclaw

    (@holsclaw)

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

    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

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘passing value through get_header to header.php’ is closed to new replies.