[wp-trac] [WordPress Trac] #55942: Add a "value type" parameter to get_option() and get_metadata()

WordPress Trac noreply at wordpress.org
Tue May 2 07:38:05 UTC 2023


#55942: Add a "value type" parameter to get_option() and get_metadata()
-------------------------------------------------+-------------------------
 Reporter:  azaozz                               |       Owner:  (none)
     Type:  enhancement                          |      Status:  new
 Priority:  normal                               |   Milestone:  6.3
Component:  Options, Meta APIs                   |     Version:
 Severity:  normal                               |  Resolution:
 Keywords:  has-patch has-unit-tests needs-      |     Focuses:
  testing                                        |  performance
-------------------------------------------------+-------------------------

Comment (by knutsp):

 Replying to [comment:61 azaozz]:

 > The (new) `$value_type` parameter in `get_option()`, `get_metadata()`,
 and related functions can be used to get the value with a specified type.
 Generally that type should match the type of the data when the option or
 meta was saved, however that is not compulsory. For example:
 >
 > 1. `add_option( 'my_option', true );` then:
 >     - `get_option( 'my_option' );` will return string `'1'`. This is the
 current behavior.
 >     - `get_option( 'my_option', false, 'bool' );` will return boolean
 `true`.
 >     - `get_option( 'my_option', false, 'int' );` will return integer
 `1`.
 > 2. `add_option( 'my_option', 0 );` then:
 >     - `get_option( 'my_option' );` will return string `'0'`. This is the
 current behavior.
 >     - `get_option( 'my_option', false, 'bool' );` will return boolean
 `false`.
 >     - `get_option( 'my_option', false, 'int' );` will return integer
 `0`.
 > 3. `add_option( 'my_option', 'some text' );` then:
 >     - `get_option( 'my_option' );` will return string `'some text'`.
 This is the current behavior.
 >     - `get_option( 'my_option', false, 'bool' );` will return boolean
 `true`.
 >     - `get_option( 'my_option', false, 'int' );` will return integer
 `0`.

 What will be the difference to writing
 `(string) get_option( 'my_option' );`
 `(bool) get_option( 'my_option' );`
 `(int) get_option( 'my_option' );`
 as we do now?

 > In practice using `$value_type` would make it possible to do something
 like:
 > {{{
 > add_option( 'my_option', true );
 > ...
 > if ( true === get_option( 'my_option', false, 'bool' ) ) {
 >    ...
 > }
 > }}}
 When the type of an expression to test in `if` is known to be bool, the
 equation with `true` is redundant and the thing for me wil be not having
 to test for equality with neither `true` nor `false`.

 From sligthly positive to this, I now in doubt. If the saved type is still
 to be guessed, is this PR then **only** to have a different way of writing
 code to get a typed value?

 The main problem I have encountered is mostly determining the difference
 between
  - `'my option'` not set/exists
  - `add_option( 'my_option', '' );`
  - `add_option( 'my_option', false );`
 What now, if the, presumed bool, option does not exist? Do I still have to
 get it without conversion (string) and then for the casted bool value in
 case it exists?

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


More information about the wp-trac mailing list