[wp-trac] [WordPress Trac] #54459: Give get_option a third parameter to prevent DB queries

WordPress Trac noreply at wordpress.org
Wed Nov 17 14:33:32 UTC 2021


#54459: Give get_option a third parameter to prevent DB queries
-----------------------------------+------------------------------
 Reporter:  joostdevalk            |       Owner:  (none)
     Type:  enhancement            |      Status:  new
 Priority:  normal                 |   Milestone:  Awaiting Review
Component:  Options, Meta APIs     |     Version:
 Severity:  normal                 |  Resolution:
 Keywords:  has-patch 2nd-opinion  |     Focuses:
-----------------------------------+------------------------------
Changes (by desrosj):

 * keywords:  has-patch => has-patch 2nd-opinion


Comment:

 I took a look and reacquainted with how `get_option()` works because I
 feel like this has come up somewhere for me in the past.

 It seems there are a few ways to accomplish this already. Because this
 change would be an opt-in change, I'm wondering if these are better
 options because they don't require additional arguments to be added.

 - Using the `pre_cache_alloptions` filter, someone could check `! isset(
 $alloptions['myoption'] )` and add the default value for that option.
 `wp_load_alloptions()` fetches all options with `autoload = 'yes'` (unless
 there are no options set to `autoload`).
 - At some point during the bootstrap process after `wp_load_alloptions()`
 has been called (or after calling it early on), a dev could run code that
 checks the keys present in the `alloptions` cache, and add their own known
 `autoload` options with default values before updating that cache.
 - The same as previous, a dev could add their keys to the `notoptions`
 cache, and the function would fall back to returning the passed default
 value without a query.

 Personally, I like option 3 because it allows a different default to be
 passed in different locations where `get_option()` is called if desired. A
 filter could be probably be added to allow someone to adjust the
 `notoptions` cache before it's updated to make this easier.

 Another way to protect these extra queries is to have a list of registered
 options. But this is really the same thing as the `alloption` list, except
 it would include the ones that don't currently exist.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/54459#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list