• Resolved uritz

    (@uritz)


    I have a function (say call_api()) which reaches out to API and stores value in a variable which I then want to share across templates. I have tried a number of ways :

    1) Using Session Variable and sharing the variable, but the function is called more than once.

    2) Using static variable to run function only once.

    Now, I want the function to run once when user loads the website, and store the value in a function which is stored in a variable which can be shared across various templates.

    I have done extensive Google Search but was unable to come up with a solution. Can anyone please point me to the right direction ?

Viewing 11 replies - 1 through 11 (of 11 total)
  • Store the variable as a theme option in the wp_options table?

    Thread Starter uritz

    (@uritz)

    Aah, but I cannot store the variable in the database, sorry I forgot to point that out.

    Why not (curious minds and all that…)?

    Thread Starter uritz

    (@uritz)

    I can as well call the API again and again. I can store the variable in the database, then for every product, I would have to make extra database calls for the variable. Plus, since the value of variable changes every second, it could and should be different for different customers, I should refrain from using the normalised value of the variable. Is there any other way ? 🙂

    Not really – especially if storing the variable in a session isn’t working for you.

    You could always try something like this (not tested, so coudl have bugs)…

    $api_value = NULL;
    
    function get_api_value () {
        global $api_value;
    
        if (is_null ($api_value)) {
            $api_value = call_api ();
        }
    
        return $api_value;
    }

    That will allow you to get the value once per page execution and share it as many times as needed.

    Thread Starter uritz

    (@uritz)

    Esmi : I tried storing the variable in session, but on every page, value in the session variable changed, could you point me how to store it in a session properly ?

    Catacaustic : Thanks for replying. I agree, I can use something like that to load a variable value once per page execution, but what I am looking for is something where I can store it for the entire time the user is on the website(session). Since, I don’t exactly know when the session gets over, it should have a timeout too. Lastly, I might even try storing the value in user’s cookie. If you have any idea in either direction or another idea, please point me towards it 🙂

    Either use a cookie, or wordpress transient.

    function get_api_value() {
        if ( false === ( $value = get_transient( 'api_value' ) ) ) {
            $value = call_api();
            set_transient( 'api_value', $value, 3600 ); // store for 3600s
        }
        return $value;
    }

    http://codex.wordpress.org/Transients_API

    Thread Starter uritz

    (@uritz)

    Yes, awesome, this works great. To test it out, I did the following :

    function get_rand_value() {
        if ( false === ( $value = get_transient( 'api_value' ) ) ) {
            $value = rand(0,10);
            set_transient( 'api_value', $value, 3600 ); // store for 3600s
        }
        return $value;
    }

    And I called get_rand_value() in footer, so that it gets called on every template. Works great man, thanks. I would still like to learn if it is possible to put it in session, just for learning sake. 🙂

    Sessions are a bit overkill, and if the server isnt setup write, session_save_path etc they can be a pain as well.
    Cookies or transients, thats my vote 😉

    Thread Starter uritz

    (@uritz)

    Yup, and its working great. 🙂

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Run a function only once – share variable across templates PHP’ is closed to new replies.