I have been trying to work out the most effecient way to use functions in WordPress.
I have a big slow function big_slow_function() that ideally is only run once. But I need to use the boolean that is returned by this function throughout my theme files (in header.php, page.php, sidebar.php, footer.php, loop-page.php, functions.php etc).
I am wondering how to do this.
I tried putting this in my functions.php to try and avoid calling big_slow_function() more than once:
global $my_important_boolean;
function $get_my_important_boolean()
{
global $my_important_boolean;
if ($my_important_boolean == NULL) // if big_slow_function() has not been run yet
$my_important_boolean = big_slow_function();
return $my_important_boolean;
}
And then I put code like this throughout my theme files:
if ($get_my_important_boolean()) {
// customize content to user
}
But the big_slow_function() is still being run every time. I am not sure what I am doing wrong and have found it hard to find good documentation on variable scope in WordPress. Perhaps I need to pass a reference/pointer to the variable?
Any help with this problem is much appreciated as I have been struggling with it for sometime.
Also see: https://stackoverflow.com/questions/6188994/static-keyword-inside-function