WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm initializing a variable with a value in the header.php file. I want access to that value in footer.php, but it doesn't exist upon checking. Example:


$status = true;


var_dump( $status ); // is null

What's a best practice for doing something in footer.php, or other files if they have the same behavior.

share|improve this question
What are you trying to accomplish? Eg. what does the $status variable do? – chrisguitarguy Oct 16 '12 at 5:24
I have some internal testing I do based on the user, if they are logged in, authenticated, if it is the front page or other, etc... the point is it seems code you use in header.php cannot be inherited so that you can use it in footer.php in this manner. I'd like access to that without having to paste all the same code that comes up with the value in $status in footer.php too. – Poe Oct 16 '12 at 5:27
You should have a look at the WordPress action/filter API, codex.wordpress.org/Plugin_API. – Wyck Oct 16 '12 at 5:33
up vote 5 down vote accepted

Avoid global variables, they could be overwritten by other code. You could use a helper function with a static internal variable instead. Sample code:

function wpse_69365_var_storage( $var = NULL )
    static $internal;

    if ( NULL !== $var )
        $internal = $var;

    return $internal;

// first call:
wpse_69365_var_storage( 4 );

// later call:
echo wpse_69365_var_storage(); // print 4
share|improve this answer
True, that should work. Thanks! – Poe Oct 16 '12 at 5:35
Nice idea on using static variable vs. global variable! – Rilwis Oct 16 '12 at 7:56

Based upon @toscho snippet, here's a real life example. Assuming that you don't need to access a var directly from inside the template file.

function wpse_69365_var_storage()
    static $internal;

    if ( 'wp_head' === current_filter() )
        if ( /* Condition is met */ )
            return $internal = 4; // Set to what you need.

        return 'default_value';

    return print $internal;
// Call in header.php:
add_action( 'wp_head', 'wpse_69365_var_storage' );
// Call in footer.php:
add_action( 'wp_footer', 'wpse_69365_var_storage' );
share|improve this answer

The WordPress functions get_header(); get_footer(); and get_sidebar(); can be replaced with native PHP includes to make variables available in your templates.

    // change the following in your template files
    get_header(); // to --> include (TEMPLATEPATH . "/header.php");
    get_footer(); // to --> include (TEMPLATEPATH . "/footer.php");
    get_sidebar(); // to --> include (TEMPLATEPATH . "/sidebar.php");

Then you should be able to access variables defined in the header.php

share|improve this answer

Your Answer


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.