Title: using variables before get_header()
Last modified: August 18, 2016

---

# using variables before get_header()

 *  Anonymous User 174961
 * (@anonymized-174961)
 * [19 years, 1 month ago](https://wordpress.org/support/topic/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:
 *     ```
       <?php
       $myVariable = "something";
       get_header();
       ?>
       ```
   
 * (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)

 *  [apmeyer](https://wordpress.org/support/users/apmeyer/)
 * (@apmeyer)
 * [19 years ago](https://wordpress.org/support/topic/using-variables-before-get_header/#post-551002)
 * 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:
 * <?php
    function myVariable(){ return = “something”; } ?>
 * Then when you want to use that “variable” in the header, sidebar or footer, you
   could do something like:
 * <?php
    if(myVariable() == “something”){ //do stuff } ?>
 * Not sure if that will help for your situation, but it works for what I needed.
 *  [Dalton Rooney](https://wordpress.org/support/users/daltonrooney/)
 * (@daltonrooney)
 * [18 years, 5 months ago](https://wordpress.org/support/topic/using-variables-before-get_header/#post-551034)
 * 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; ?>
       ```
   
 *  [websta](https://wordpress.org/support/users/websta/)
 * (@websta)
 * [18 years, 5 months ago](https://wordpress.org/support/topic/using-variables-before-get_header/#post-551035)
 * 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.
 *  [Dalton Rooney](https://wordpress.org/support/users/daltonrooney/)
 * (@daltonrooney)
 * [18 years, 5 months ago](https://wordpress.org/support/topic/using-variables-before-get_header/#post-551036)
 * Hey Websta – you don’t need to pass global variables to do that. You need to 
   look into category templates: [http://codex.wordpress.org/Category_Templates](http://codex.wordpress.org/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:
    [http://codex.wordpress.org/Template_Tags/in_category](http://codex.wordpress.org/Template_Tags/in_category)
 * That’s probably the smarter way to do it. More PHP, less template files to maintain.
 *  [websta](https://wordpress.org/support/users/websta/)
 * (@websta)
 * [18 years, 5 months ago](https://wordpress.org/support/topic/using-variables-before-get_header/#post-551037)
 * 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.

## Tags

 * [get_header](https://wordpress.org/support/topic-tag/get_header/)
 * [header](https://wordpress.org/support/topic-tag/header/)
 * [variables](https://wordpress.org/support/topic-tag/variables/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 5 replies
 * 4 participants
 * Last reply from: [websta](https://wordpress.org/support/users/websta/)
 * Last activity: [18 years, 5 months ago](https://wordpress.org/support/topic/using-variables-before-get_header/#post-551037)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
