Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

UPDATE:

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:

<?php
/**
 * 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(); ?>
</body>
</html>

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

share|improve this question
2  
Ever heard of global and $GLOBALS? –  kaiser Sep 18 '13 at 9:14
    
@kaiser Ever noticed the bold text in my question? –  Derfder Sep 18 '13 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 Sep 18 '13 at 9:30
3  
global $sidebar_state; go before you set the var and before using it. –  G. M. Sep 18 '13 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; ?> –  G. M. Sep 18 '13 at 10:21

3 Answers 3

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

share|improve this answer
    
Thanks, I think this would be the problem. I will try to go from here and I will let you know. –  Derfder Sep 18 '13 at 9:41
    
+1 this is the problem. –  kaiser Sep 18 '13 at 10:01
    
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 Sep 18 '13 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 Sep 18 '13 at 10:07
1  
just wondering, what are you trying to achieve? What's that sidebar state that you're trying to get? –  Got The Fever Media Sep 18 '13 at 10:37

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.

share|improve this answer
2  
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 Sep 18 '13 at 16:02

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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