Passing variables from header.php to template and vice verca

I’ve defined a variable – let’s call it $header_var, which is placed in the header.php file. I would like this variable to be passed onto my template file (in this case taxonomy.php).

Also I would like to be able to do the same the other way around, like passing a $template_var from my taxonomy.php to header.php.

Is this even possible since the variable was not declared once the header was loaded?

I’ve tried using global $header_var but without luck.

Any suggestions?

Solutions Collecting From Web of "Passing variables from header.php to template and vice verca"

I’ve defined a variable – let’s call it
$header_var, which is placed in the header.php file. I would like this
variable to be passed onto my template file (in this case
taxonomy.php).

global is not a very recommended way to do the trick, but it works if you use it properly: put it before to define the variable in header.php and again before retrieve the variable in taxonomy.php (after having called get_header() to include header.php)

// in header.php
global $header_var;
$header_var = 'A value';

// in taxonomy.php
get_header();
global $header_var;
echo $header_var; // 'A value'

I would like to be able to do the same the other way around, like
passing a $template_var from my taxonomy.php to header.php. Is this
even possible since the variable was not declared once the header was
loaded?

It’s PHP, not magic nor time machine: time rules apply to WordPress like to the rest of the universe.

So, no, you can’t pass a variable back in time, but usually, in templates, you include header.php by calling get_header() so if you set a variable before calling that function, the global trick will work too:

// in header.php
global $template_var;
echo $template_var; // 'A value'

// in taxonomy.php
global $template_var;
$template_var = 'A value'
get_header();

However if you need to share variables in header.php and templates, best thing to do is do not declare them in header nor in templates, but in functions.php using action hooks to control when variables have to be declared.

An useful hook to is 'template_redirect' where you have access to current query and it is fired before header.php and templates are loaded.

A rough example:

// in functions.php
add_action( 'template_redirect', 'get_my_shared_vars' );

function get_my_shared_vars() {
   static $shared_vars = NULL;
   if ( empty( $shared_vars ) ) {
     $shared_vars = array( 'header_var' => 'An header value' );
     if ( is_tax() || is_category() || is_tag() ) {
       $shared_vars['taxonomy_var'] = 'A taxonomy value';
     }
   }
   return $shared_vars;
}


// in header.php
$shared_vars = get_my_shared_vars();
echo $shared_vars['header_var']; // 'An header value'

// in taxonomy.php
$shared_vars = get_my_shared_vars();
echo $shared_vars['taxonomy_var']; // 'A taxonomy value'

In previous code, thanks to static keyword all the code in get_my_shared_vars function used to set variables runs only once, so you don’t have to worry about performance issues if calling the function more than once.