wp/wp-includes/option.php
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
     7  */
     7  */
     8 
     8 
     9 /**
     9 /**
    10  * Retrieves an option value based on an option name.
    10  * Retrieves an option value based on an option name.
    11  *
    11  *
    12  * If the option does not exist or does not have a value, then the return value
    12  * If the option does not exist, and a default value is not provided,
    13  * will be false. This is useful to check whether you need to install an option
    13  * boolean false is returned. This could be used to check whether you need
    14  * and is commonly used during installation of plugin options and to test
    14  * to initialize an option during installation of a plugin, however that
    15  * whether upgrading is required.
    15  * can be done better by using add_option() which will not overwrite
    16  *
    16  * existing options.
    17  * If the option was serialized then it will be unserialized when it is returned.
    17  *
    18  *
    18  * Not initializing an option and using boolean `false` as a return value
    19  * Any scalar values will be returned as strings. You may coerce the return type of
    19  * is a bad practice as it triggers an additional database query.
    20  * a given option by registering an {@see 'option_$option'} filter callback.
    20  *
       
    21  * The type of the returned value can be different from the type that was passed
       
    22  * when saving or updating the option. If the option value was serialized,
       
    23  * then it will be unserialized when it is returned. In this case the type will
       
    24  * be the same. For example, storing a non-scalar value like an array will
       
    25  * return the same array.
       
    26  *
       
    27  * In most cases non-string scalar and null values will be converted and returned
       
    28  * as string equivalents.
       
    29  *
       
    30  * Exceptions:
       
    31  * 1. When the option has not been saved in the database, the `$default` value
       
    32  *    is returned if provided. If not, boolean `false` is returned.
       
    33  * 2. When one of the Options API filters is used: {@see 'pre_option_{$option}'},
       
    34  *    {@see 'default_option_{$option}'}, or {@see 'option_{$option}'}, the returned
       
    35  *    value may not match the expected type.
       
    36  * 3. When the option has just been saved in the database, and get_option()
       
    37  *    is used right after, non-string scalar and null values are not converted to
       
    38  *    string equivalents and the original type is returned.
       
    39  *
       
    40  * Examples:
       
    41  *
       
    42  * When adding options like this: `add_option( 'my_option_name', 'value' );`
       
    43  * and then retrieving them with `get_option( 'my_option_name' );`, the returned
       
    44  * values will be:
       
    45  *
       
    46  * `false` returns `string(0) ""`
       
    47  * `true`  returns `string(1) "1"`
       
    48  * `0`     returns `string(1) "0"`
       
    49  * `1`     returns `string(1) "1"`
       
    50  * `'0'`   returns `string(1) "0"`
       
    51  * `'1'`   returns `string(1) "1"`
       
    52  * `null`  returns `string(0) ""`
       
    53  *
       
    54  * When adding options with non-scalar values like
       
    55  * `add_option( 'my_array', array( false, 'str', null ) );`, the returned value
       
    56  * will be identical to the original as it is serialized before saving
       
    57  * it in the database:
       
    58  *
       
    59  *    array(3) {
       
    60  *        [0] => bool(false)
       
    61  *        [1] => string(3) "str"
       
    62  *        [2] => NULL
       
    63  *    }
    21  *
    64  *
    22  * @since 1.5.0
    65  * @since 1.5.0
    23  *
    66  *
    24  * @global wpdb $wpdb WordPress database abstraction object.
    67  * @global wpdb $wpdb WordPress database abstraction object.
    25  *
    68  *
    26  * @param string $option  Name of the option to retrieve. Expected to not be SQL-escaped.
    69  * @param string $option  Name of the option to retrieve. Expected to not be SQL-escaped.
    27  * @param mixed  $default Optional. Default value to return if the option does not exist.
    70  * @param mixed  $default Optional. Default value to return if the option does not exist.
    28  * @return mixed Value set for the option.
    71  * @return mixed Value of the option. A value of any type may be returned, including
       
    72  *               scalar (string, boolean, float, integer), null, array, object.
       
    73  *               Scalar and null values will be returned as strings as long as they originate
       
    74  *               from a database stored option value. If there is no option in the database,
       
    75  *               boolean `false` is returned.
    29  */
    76  */
    30 function get_option( $option, $default = false ) {
    77 function get_option( $option, $default = false ) {
    31 	global $wpdb;
    78 	global $wpdb;
    32 
    79 
    33 	$option = trim( $option );
    80 	$option = trim( $option );
  1055 /**
  1102 /**
  1056  * Retrieves user interface setting value based on setting name.
  1103  * Retrieves user interface setting value based on setting name.
  1057  *
  1104  *
  1058  * @since 2.7.0
  1105  * @since 2.7.0
  1059  *
  1106  *
  1060  * @param string $name    The name of the setting.
  1107  * @param string       $name    The name of the setting.
  1061  * @param string $default Optional default value to return when $name is not set.
  1108  * @param string|false $default Optional. Default value to return when $name is not set. Default false.
  1062  * @return mixed The last saved user setting or the default value/false if it doesn't exist.
  1109  * @return mixed The last saved user setting or the default value/false if it doesn't exist.
  1063  */
  1110  */
  1064 function get_user_setting( $name, $default = false ) {
  1111 function get_user_setting( $name, $default = false ) {
  1065 	$all_user_settings = get_all_user_settings();
  1112 	$all_user_settings = get_all_user_settings();
  1066 
  1113 
  2320  *              Please consider writing more inclusive code.
  2367  *              Please consider writing more inclusive code.
  2321  *
  2368  *
  2322  * @global array $new_allowed_options
  2369  * @global array $new_allowed_options
  2323  * @global array $wp_registered_settings
  2370  * @global array $wp_registered_settings
  2324  *
  2371  *
  2325  * @param string   $option_group The settings group name used during registration.
  2372  * @param string          $option_group The settings group name used during registration.
  2326  * @param string   $option_name  The name of the option to unregister.
  2373  * @param string          $option_name  The name of the option to unregister.
  2327  * @param callable $deprecated   Deprecated.
  2374  * @param callable|string $deprecated   Deprecated.
  2328  */
  2375  */
  2329 function unregister_setting( $option_group, $option_name, $deprecated = '' ) {
  2376 function unregister_setting( $option_group, $option_name, $deprecated = '' ) {
  2330 	global $new_allowed_options, $wp_registered_settings;
  2377 	global $new_allowed_options, $wp_registered_settings;
  2331 
  2378 
  2332 	/*
  2379 	/*