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.

I added a variable to my functions.php file:

$options = get_option( 'simple_options' );

I can access to this variable on every page, but If I call it in the footer.php it's not working. How to fix that?

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

Generally, you cannot access local variables in a function from outside the function, so this...

function func() {
    $var = 2;
}

$var = 1;
func();
echo $var;

would result in "1".

To make a global variable, you can do this to make $option available outside functions.php:

function func() {
    global $option;
    $option = get_option( 'simple_options' );
} 

However, I cannot recommend it, since every other script may change the value. You can use a define:

define('MY_OPTION',get_option( 'simple_options' );

And in your footer:

echo MY_OPTION; // note that there are no quotation marks

But also this wouldn't be that neat, since you'd have to make sure no other script defines this MY_OPTION and you cannot change it later on.

No, your best bet is to get the option just again in your footer.

share|improve this answer
add comment

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.