E.g. I set this in header.php in my template:

<?php $postidextra = get_the_ID(); ?> 
<?php $sidebar_state = get_post_meta($postidextra , 'rw_sidebar_state', true); ?>

And I can echo $sidebar_state in header.php, it's working.

However, when I try to echo this variable from footer.php or page.php it's not working and I need to repeat my code to get the post id and post meta etc. which is repeating and a bad practice.

Why it's not available if it is just include?

How to make the variable available in all files across WP template files?


After suggestion to use global variables I am still unable to echo $sidebar_state; in footer.php. My code in header.php looks like this:

 <?php global $postidextra; ?> 
 <?php $postidextra = get_the_ID(); ?>
 <?php global $sidebar_state; ?>
 <?php $sidebar_state = get_post_meta($postidextra , 'rw_sidebar_state', true); ?>

Footer.php looks like this:

 * The template for displaying the footer.
 * Contains footer content and the closing of the
 * #main and #page div elements.
 * @package WordPress
 * @subpackage Twenty_Twelve
 * @since Twenty Twelve 1.0
    </div><!-- #main .wrapper -->
    <footer id="colophon" role="contentinfo">
        <div class="site-info">
            <?php echo $sidebar_state; ?>
        </div><!-- .site-info -->
    </footer><!-- #colophon -->
</div><!-- #page -->

<?php wp_footer(); ?>

<?php echo $sidebar_state; ?> is wokring only in header.php . Any idea what should I change in my code?

  • 2
    Ever heard of global and $GLOBALS?
    – kaiser
    Commented Sep 18, 2013 at 9:14
  • @kaiser Ever noticed the bold text in my question?
    – Derfder
    Commented Sep 18, 2013 at 9:25
  • @kaiser and even if I do global $sidebar_state; it's not working. Please, explain who do you mean exactly and how should it be done, instead of downvoting without a reason.
    – Derfder
    Commented Sep 18, 2013 at 9:30
  • 3
    global $sidebar_state; go before you set the var and before using it.
    – gmazzap
    Commented Sep 18, 2013 at 9:35
  • 2
    Read my comment: "before you set the var and before using it". <?php global $postidextra; $postidextra = "20"; ?> and then <?php global $postidextra; echo $postidextra; ?>
    – gmazzap
    Commented Sep 18, 2013 at 10:21

3 Answers 3


All I need was to use global variable before the declaration AND before echoing.

<?php global $postidextra; $postidextra = "20"; ?>

and then

<?php global $postidextra; echo $postidextra; ?>

Thanks G.M. for the idea.

  • 3
    fyi, the reason this is necessary is because the header and footer are included via functions, placing the variable only in scope of each function if it's not globalized within each.
    – Milo
    Commented Sep 18, 2013 at 16:02

the codex states that you can only use get_the_id() in the Loop

  • Thanks, I think this would be the problem. I will try to go from here and I will let you know.
    – Derfder
    Commented Sep 18, 2013 at 9:41
  • Hmm, I have tried <?php global $postidextra; ?> <?php $postidextra = $wp_query->post->ID; ?> and <?php echo $postidextra; ?> is still only working in header.php . and not in footer.php ;(. What am I doing wrong?
    – Derfder
    Commented Sep 18, 2013 at 10:06
  • @kaiser Even if I do <?php global $postidextra; ?> <?php $postidextra = "20"; ?> and <?php echo $postidextra; ?> again, it's still only working in header.php and not in footer.php . Why can't I use the $postidextra variable in footer.php if it is set as global in header.php? I am using standard WP template (Twenty Twelve 1.0), that comes with WP
    – Derfder
    Commented Sep 18, 2013 at 10:07
  • 1
    just wondering, what are you trying to achieve? What's that sidebar state that you're trying to get? Commented Sep 18, 2013 at 10:37

The reason that a declared global variable in header.php cannot be accessed in footer.php is because header.php and footer.php are template-part files that are included in a given template file via get_header() and get_footer(), respectively.

Each of these template tags is a fancy wrapper for the PHP include() function. In PHP, global variables are not passed to included files, and thus cannot be referenced without being redeclared.

The solution is simply to declare the global variable in footer.php before referencing it.

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.