Skip to:
Content
Pages
Categories
Search
Top
Bottom

Global variables not accessible from theme files

  • @jakubmarian

    Participant

    Hello, I am trying to integrate bbPress seamlessly into my custom WordPress theme. However, this theme defines several global variables which are then used by the theme files.

    These variables are defined as global variables in header.php of the wordpress theme and are used without problems in all theme files. Example:

    header.php
    global $variable;
    $variable = "value";

    single.php
    global $variable;
    echo $variable; // outputs "value" as it should

    So far, so good. But if I try to use the variable in a bbPress theme file, it is empty:

    content-single-topic.php
    global $variable;
    echo $variable; // outputs ""

    When bbPress is done, the variable comes back to life:

    loop-single.php
    echo $variable; // outputs "value" as it should
    the_content(); // all the bbPress stuff, $variable empty – what happens there?
    echo $variable; // outputs "value" again

    Using print_r($GLOBALS), it turns out that in fact all the custom variables cease to exist for the time bbPress is doing its job and than come back to life.

    How can I pass a global value to the bbPress theme files without doing absurd things like querying the database?

Viewing 5 replies - 1 through 5 (of 5 total)
  • @lynqoid

    Participant

    Could you not use update_option and get_option? You could serialise an array and then add it to options?

    @jakubmarian

    Participant

    I could, but adding two database queries (that’s what update/get_option() does) for each variable I need to pass to the theme files is ridiculous; this could considerably hurt the website performance.

    Even worse, it could even completely fail if several users used the website at the same time, because the option has just one common value for all users. If the script preparing the website for another user rewrites the value before your script reaches the point where the value is read from the database, you get a wrong value.

    @lynqoid

    Participant

    Could you create a class then initalize it inside the bbPress code and use a getter to grab the values you need?

    @jakubmarian

    Participant

    But how do I make the values accessible to the class? If the getter just gets the value from global variables, it will see them as empty as well. I think I’ll just write another version of the script used in the header and include it in the theme files. It’s silly, because some of the work will have to be done twice every time the forum is loaded, but it is still the least performance-hurting way to do that.

    @lynqoid

    Participant

    Good luck! 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.
Skip to toolbar