• I need persistent variables such as global variables, but using global variables in global scope(out of functions) is prohibited in developing theme. So I wonder how could I make the value of my variables persistent throughout my theme(from the start to the end).
    Actually I’m trying to accumulate(or concatenate) strings or array values passed through a parameter in the variable in a function whenever the function is called, and then print that string or serialized array values at the footer as follows:

    function entire_string( $string ) {
        static $entire_string = '';
        $entire_string = ' '.$string;
    }
    
    entire_string('foo');
    entire_string('bar');
    
    add_action('wp_footer', 'print_entire_string');
    function print_entire_string() {
        global $entire_string;
        print_r($entire_string);  // But it does not work! This returns ' '.
    }

    I have no idea what the problem is. Please teach me how I can achieve my goal, please!

    (P.S: Using global $entire_string; with initialization of the variable($entire_string = '';) outside of all functions instead of static $entire_string = ''; works just fine!)

  • The topic ‘persistent variables for a theme’ is closed to new replies.