Support » Fixing WordPress » using variables before get_header()

  • Hello, i’ve been setting up a WordPress site for the first time and things’ve been pretty smooth so far, but now i’m stuck. I’ve found a few things on-line regarding this subject, but i guess it hasn’t helped, ’cause i can’t seem to get it.

    What i’m trying to do is get the side bar or header to use a variable declared at the beginning of the page (say index.php) that calls it. In other words, something like:

    $myVariable = "something";

    (and then obviously there is something in header.php that needs to use that variable)

    I understand that it isn’t going to be as simple as what i wrote above because of the scope of the variable and all that stuff. I’ve been trying to use hooks or actions or whatever, but i still can’t get it. I can put <?php wp_head(); ?> in my header.php and then put some html inside a function and use add_action() to add that to wp_head()… and that works fine. The HTML gets displayed in the header. But i can not get it to just set a variable. It stays empty.

    Does anyone know how i could do this? Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • I am guessing you probably found a solution by now, but…

    I was trying to do the same thing. It wouldn’t work. The work around I used was creating a function before the get_header() tag. While the content in the header wouldn’t recognize variables set previously, it would make a call to a function set previously.

    So, in your case you could create a function like:

    function myVariable(){
    return = “something”;

    Then when you want to use that “variable” in the header, sidebar or footer, you could do something like:

    if(myVariable() == “something”){
    //do stuff

    Not sure if that will help for your situation, but it works for what I needed.

    Here is a much easier way, if anyone else is looking for a solution for this.

    In index.php, or other template you’re using:

    <?php global $your-variable;
    $your-variable = the-value;
    get_header(); ?>

    Then in your header:

    <?php global $your-variable;
    echo $your-variable; ?>

    daltonrooney, could your solution be configured to pull a different header file (header-1.php, header-2.php…) depending which category the post is in?

    I am wondering if a site has 40 categories or whatever, but a set of main categories (say, 1-8) could be set up for the different headers?

    Do you know how? Sorry, I am a newbie with PHP and am trying to figure out how to have a different color of header for the major categories… I’m confess the multiple conditions are beyond me.

    Hey Websta – you don’t need to pass global variables to do that. You need to look into category templates:

    You can have a unique template for each category. The easy way to do it is just call a unique header file in each category template.

    You should also take a look at the “in_category” template tag. You could create a header that looks to see if the post is in a specific category and outputs the appropriate code at that time:

    That’s probably the smarter way to do it. More PHP, less template files to maintain.

    Thanks daltonrooney. I will try that out.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘using variables before get_header()’ is closed to new replies.