(Global) variable issue
-
I have an issue concerning (global?) variables. Some pseudo code:
function my_plugin_install() { // glad, we have nothing to do here... } add_action ('my_plugin/my_plugin.php', 'my_plugin_install'); global $my_var; $my_var = ""; function my_function() { global $my_var; $my_var = "true"; return $my_var; } add_filter ('some_foreign_call_from_another_plugin', 'my_function'); function write2header() { global $my_var; echo "Within our header: " . $my_var; } add_action ('admin_head', 'write2header');
What do I want to do?
My plugin is called by some_foreign_call_from_another_plugin‘s filter hook and I immediately call my_function. That works well. Within that function, I set the variable $my_var to “true”. That variable is defined as global earlier.
Later in my plugin, I want to insert some code into the <HEAD> part of the output. And I want to use $my_var there.
But that’s the one thing, that doesn’t work. Within the header, it says: “Within our header: ” – but nothing more. No output of $my_var.
Why? What am I doing wrong?
Do I have to define my global variable later? Earlier? Where?
Should I use $GLOBALS[“my_var”]? I already tried a little bit with that – didn’t work either. ??
Thomas
- The topic ‘(Global) variable issue’ is closed to new replies.