I’m trying to display a variable that is stored inside my functions.php file – for the sake of the question this variable is stored as $test = 'test';
. When i use echo $test
inside page.php, header.php or any other file the value is returned however when i try to do the same inside a widget (i’m using a plugin that allows execution of PHP inside a widget) nothing happens.
Any ideas as to how i could get around this?
The widget operates in a different scope than the
functions.php
.You could use two different approaches to get around that.
Make the variable global (put it into the top scope):
But that is risky: any other script can change the variable now accidentally, and it is very hard to debug this.
Create a special class or function for the variable. You could even use one class or function to store many values. Example:
In your
functions.php
, you can create an object now and add a value:In your widget, you can get that object and the stored value:
You can even create multiple independent
Theme_Data
objects for different purposes, just create them with different$filter
strings: