• I am having a problem with using my own global variables in a plugin. I want to save a value into a global variable in a plugin, and then echo that value in header.php.

    In my plugin I declare the global like this at the top of the file:

    global $dibbit_value ;

    and then set it inside a function like this:

    function xyz()
    global $dibbit_value ;
    $dibbit_value = "test" ;

    If I try and echo $dibbit_value in header.php it just comes up blank.

    If I set $dibbit_value at the top of my plugin as soon as I declare it, it works ok, but not if I try and set it within a function.

    What am I doing wrong?

    Thx

Viewing 3 replies - 1 through 3 (of 3 total)
  • Might i suggest some further reading on general PHP.

    Writing functions:
    http://www.php.net/manual/en/functions.user-defined.php

    Variable scope:
    http://php.net/manual/en/language.variables.scope.php

    MG7282

    (@mgrinshpun)

    I am having a similar issue right now. I defined a variable as global within my function who’s hook is init, but I am unable to print the variable in the header.. Any suggestions?

    Thread Starter dibbit

    (@dibbit)

    I must have been having brain fade when I posted on this back in December, as you clearly can’t use a variable before you have set its value…

    Two possible solutions:

    1) Use GET or POST data to communicate with the header part of the page.

    2) Save the value you want to pass in a custom field somewhere and retrieve it in the header. This will only work if the value is the same for everyone who looks at your website.

    Hope that helps.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Using Global variables in a plugin’ is closed to new replies.