Performance with autoload and the options table

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?

  1. Is it okay that I retrieve it from the $GLOBALS array(since the value is already here)
  2. Must I use get_option('my_option') once and globalize the variable again
  3. Use get_option('my_option') each time (which I don’t see the point of doing)
  4. Something else

1

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?

  1. It is bad approach to retrieve it from $GLOBALS directly, use get_option instead
  2. Globalize custom variables is not welcome and recommended to do it as less as possible
  3. From my point of view it’s the best option for you
  4. Even don’t think about something else 🙂

Why to use get_option function each time is the best option? Because:

  1. The first and the most important because: you can be confident that it will return up-to-date value, each time you call it.
  2. It will cache it for future reuse.
  3. It allows other plugin and theme to hook your option by hooking option_optionname filter.
  4. It will fetch an option if it is not loaded yet.

Leave a Comment