11

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?

2 Answers 2

30

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.

2
  • 3
    If I could I would vote 1000. This should be in WP template docs
    – Benn
    Commented Dec 1, 2017 at 17:14
  • It's need to declare global in the other file I declare it on in header.php and call the variable directly.
    – Salem F
    Commented Feb 1, 2022 at 9:33
6

Wordpress 5.5+

Since 5.5.0 get_header has $args parameter

get_header changelog

get_header( 'foo', [ 'header_var' => 'A value' ] ); // header-foo.php will be loaded

/**
 * OR
 */

get_header( null, [ 'header_var' => 'A value' ] ); // header.php will be loaded

header.php

<?php echo $args['header_var']; ?>

/**
 * OR
 */

<?php extract($args); ?>
<?php echo $header_var; ?>

/**
 * Or with defaults
 */

$defaults = array( 
    'header_var2' => 'bar',
    // ...
);
extract(wp_parse_args( $args, $defaults ));

<?= $header_var ?>
<?= $header_var2 ?>
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.