I am playing around with the autoload
column of the options
table. I didn’t find much information about how the autoloaded values are used. I tried a print_r($GLOBALS)
and saw that the autoloaded options are stored in $GLOBALS['wp_object_cache']->cache['options']['alloptions']
.
Is there another way to access those variables?
Suppose I need to access an option my_option
, which was set to autoload, multiple times in different templates (eg. once in header.php
, once in footer.php
), which method is recommended?
- Is it okay that I retrieve it from the
$GLOBALS
array(since the value is already here) - Must I use
get_option('my_option')
once and globalize the variable again - Use
get_option('my_option')
each time (which I don’t see the point of doing) - Something else
I didn’t find much information about how the autoloaded values are
used.
There is no special case for autoloaded options, they are used in the same way as else regular options, but lets figure out what autoload
column of the options
table means. This column determines do we need to fetch an option at the initialization stage of a request or should we fetch an option only on demand.
But when this autoloading happens and what function does it? All autoload options are loaded and cached by wp_load_alloptions
function, which is called by is_blog_installed
function at the beginning of each HTTP request, handled by WP.
Lets summarize: autoloaded option is the same option, but loaded at the beginning of HTTP request processing by WP.
Suppose I need to access an option … which method is recommended?
- It is bad approach to retrieve it from
$GLOBALS
directly, useget_option
instead - Globalize custom variables is not welcome and recommended to do it as less as possible
- From my point of view it’s the best option for you
- Even don’t think about something else 🙂
Why to use get_option
function each time is the best option? Because:
- The first and the most important because: you can be confident that it will return up-to-date value, each time you call it.
- It will cache it for future reuse.
- It allows other plugin and theme to hook your option by hooking
option_optionname
filter. - It will fetch an option if it is not loaded yet.