facebooktwitterskypeRSS Feed
telephone: (920) 215-1842
facsimile: (866) 456-2115

Passing variables through get_header and get_footer

email

While working on a integration between a custom shopping cart solution and WordPress I found the get_header() and get_footer() to not correctly with variables and functions I needed to call.

 

I have two possible solutions:

First solution.

I can include the needed PHP file directly. This is not the preferred method but a solution at this time to pass all the needed strings and variables.

include(TEMPLATEPATH.'/header.php')

 

Second Solution.

Use variables before get_header(). In index.php, or other template file name you’re using:

<?php
$your-variable = the-value;
get_header(); 
?>

Then in your header:

<?php
echo $your-variable; 
?>

3 Responses

  1. I tried the second solution, and it seems is not working with me I don’t know.
    I using WordPress 3.0.

    But, I tried another thing though I added global before the $your-variable in the header.
    as result here whats is looks like in the header.

    global $your-variable;
    echo $your-variable;

  2. Aabdullah Radwan on Aug. 24th, 2010 at 2:44 pm
    Reply
  3. Neither of these variable passing options seem to work if you’re trying to have WP as one part of an existing site structure that utilises a common header & footer over all pages. It’s a catch 22. Either you pour your code into the WP theme’s header & footer files (ie. duplication) or you accept that you can’t have variables passed to your common header & footer files, so you create another set just for WP (ie. duplication).

    I think WP didn’t play with the other children very well as a kid. :)

  4. Dave Lock on Jun. 18th, 2011 at 8:44 am
    Reply
    • Hello Dave, I will be creating a new post explaining a solution that does work without duplication. It is a little technical but I have been testing it for about a year and it is installed in about 10 websites I have developed. Basically it uses a database table to store the users session and is then retrieved inside WordPress or outside it.

    • Jason Gegere on Jun. 30th, 2011 at 10:26 am
      Reply

Post a Comment

Do you have a question or comment about this post. Please share! Your email address will not be published. Required fields are marked *

*