WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Run a function only once - share variable across templates PHP (12 posts)

  1. uritz
    Member
    Posted 8 months ago #

    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 ?

  2. esmi
    Forum Moderator
    Posted 8 months ago #

    Store the variable as a theme option in the wp_options table?

  3. uritz
    Member
    Posted 8 months ago #

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

  4. esmi
    Forum Moderator
    Posted 8 months ago #

    Why not (curious minds and all that...)?

  5. uritz
    Member
    Posted 8 months ago #

    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 ? :)

  6. esmi
    Forum Moderator
    Posted 8 months ago #

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

  7. catacaustic
    very awesome
    Posted 8 months ago #

    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.

  8. uritz
    Member
    Posted 8 months ago #

    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 :)

  9. Simon Prosser
    Theme Review Admin
    Posted 8 months ago #

    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

  10. uritz
    Member
    Posted 8 months ago #

    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. :)

  11. Simon Prosser
    Theme Review Admin
    Posted 8 months ago #

    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 ;)

  12. uritz
    Member
    Posted 8 months ago #

    Yup, and its working great. :)

Reply

You must log in to post.

About this Topic