Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting variable in header.php but not seen in footer.php

Tags:

wordpress

in wordpress , i set a variable in header.php

<?php
$var= 'anything'
?>

but in footer.php when I echo it

<?php
echo $var;
?>

I got no thing printed ... why !>

like image 338
Naughty.Coder Avatar asked Mar 12 '10 17:03

Naughty.Coder


People also ask

Where is footer php located?

In the left-hand menu, go to Appearance -> Editor. Click on Editor. In the list of theme files on the right side, search for the file named Theme Footer (footer. php).

How do I access the header php file in Wordpress?

To find the file and edit it yourself go to wp-content > themes > your-theme-name > header. php. You can then open it in a code editor and make any changes you need to.


1 Answers

You're not in the same scope, as the header and footer files are included in a function's body. So you are declaring a local variable, and referring to another local variable (from another function).

So just declare your variable as global:

$GLOBALS[ 'var' ] = '...';

Then:

echo $GLOBALS[ 'var' ];
like image 58
Macmade Avatar answered Oct 20 '22 16:10

Macmade