I tried including my file in header.php or my theme’s index.php, but I cannot access the variables in the included file from let’s say my theme’s footer.php or even my theme’s page templates.
Here’s what I’m including:
<?php
// some parameters
$var_research = 5;
$var_researchtrans = 7;
$var_output = 9;
$var_edit_indi = 11;
$var_contact = 15;
$var_transition = 19;
?>
Now what I need is to be able to use the variables in footer.php, for example.
Hope someone out there has an answer. Thanks, y’all.
OK, here’s how I made it work:
In functions.php
<?php // functions.php
// ...
function my_var($va_var) {
// some parameters
$var_research = 5;
$var_researchtrans = 7;
$var_output = 9;
$var_edit_indi = 11;
$var_contact = 15;
$var_transition = 19;
$var_sometext = "text test";
eval("\$return_var = $" . $va_var . ";");
return $return_var;
}
?>
and in footer.php
<?php // footer.php
// ...
echo "blah blah " . my_var("var_sometext");
// ...
?>
It’s working, but did I do it right? is there a better/right way to do this? Thanks again, everyone.
The best place to include your own functions are in your theme’s
functions.phpfile.If you want to access a variable in multiple files, you can create a function in your
functions.phpand access that function anywhere within the theme.In functions.php
And in your footer.php
For your updated query