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

WordPress Trac noreply at wordpress.org
Mon May 1 23:07:03 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 azaozz):

 Replying to [comment:60 rjasdfiii]:
 > provide a couple of real-life examples

 Yep, good idea. There should be a dev. note around beta1 time too but
 couple of examples here would be nice.

 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 `1`.

 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' ) ) {
    ...
 }
 }}}

 {{{
 add_option( 'my_option', 4.7 );
 ...
 if ( get_option( 'my_option', false, 'int' ) > 5 ) {
     ...
 }

 if ( get_option( 'my_option', false, 'float' ) > 4.5 ) {
     ...
 }
 }}}

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


More information about the wp-trac mailing list